sa001-Customizing 1-pixel-wide lines with ibinspectable and constraints (view)

Source: Internet
Author: User

Note that the solution in this article assumes that you:

A. Use storyboard or xib;

B. Use AutoLayout automatic layout;

c. Use Swift rather than objective-c language.


when it comes to product development, we often encounter UI designers giving a 1 pixel wide spacer line. And this

A line of 1 pixels wide, usually we can drag a view to set its width or height to 1. This is not

There's no problem with the retina screen: 1 pixels exactly corresponds to 1 points in our programming, but in the retina

A point is mapped to 2 pixels, and when you set a constraint or frame in storyboard, you are

You cannot set a value less than 1, that is, the system default minimum unit is 1. What should I do?

at present I think of such a method: In the case of using AutoLayout, in storyboard, I'm

We lay out our spacers in a point position, where we use a normal uiview to width

Or the height constraint is set to 1 if the horizontal line is set to a height of 1, or if the vertical bar is set to a width of 1. Then we will

The class name of this view is modified to our custom class name: Sa01borderlineview (inherited from UIView).

then the overloaded method in this class layoutsubviews , traverse its constraints to find a fixed width or a fixed height of about

To dynamically modify the height or width of the constraint, depending on whether the screen is a Retina screen or not.

in the example code below, we'll look at the normal view to see if we've achieved the desired effect:

first customize a sa01borderlineview, directly inherit from UIView, overload UIView layoutsubviews

method, traversing its constraints, to find the constraint of a fixed width or height (depending on whether the line is currently horizontal or vertical). Generation

Code is as follows:

@IBDesignable class Sa01borderlineview:uiview {    @IBInspectable var Ishorizontal:bool = False    override func Layo Utsubviews () {        super.layoutsubviews () let        layoutattribute = self.ishorizontal? NSLayoutAttribute.Height:NSLayoutAttribute.Width for        item in Self.constraints () {let            constraint = Item as Nsla Youtconstraint            if constraint.firstitem as UIView = = Self && Constraint.firstattribute = = Layoutattribute { C10/>self.removeconstraint (constraint)                constraint.constant = 1/uiscreen.mainscreen ().                scale Self.addconstraint (Constraint)}}}    

Note that the @IBInspectable and @IBDesignable are used in the code , and these two keywords are swift

Newly introduced, custom view space for visual customization in Xib or storyboard new

Variables and the ability to render in real time. This is mainly because we've introduced a new variable that determines if it's a horizontal line.

: IsHorizontal, want to storyboard in the use of this view when you can directly modify in storyboard

The value of it. The screenshot is as follows:


After using the Ibinspectable keyword, the relevant property values can be modified directly in the storyboard (Method 1: Set through runtime)



After using the Ibinspectable keyword, you can modify the relevant property values directly in the storyboard (Method 2: Set through the Class property sheet)


Run:



Sample code GitHub Address: Https://github.com/lihux/iLihuxDailyAccumulates, the warehouse contains

Two projects, the source code in the Swift Project accumulates->a001 folder.

sa001-Customizing 1-pixel-wide lines with ibinspectable and constraints (view)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.