How do I create a 1px line in Interface Builder?

后端 未结 10 1732
独厮守ぢ
独厮守ぢ 2020-12-04 10:30

Note, I\'m looking to make a 1px line, not a 1pt line. Meaning it should be 1px regardless of screen scale (so 0.5pt on Retina devices).

I can do this programmatica

相关标签:
10条回答
  • 2020-12-04 11:13

    By creating this tiny subclass of NSLayoutConstraint I'm now able to add 1px lines in IB:

    @implementation NSLayoutConstraintHairline
    
    -(void)awakeFromNib
    {
        [super awakeFromNib];
        if ( self.constant == 1 ) self.constant = 1/[UIScreen mainScreen].scale;
    }
    
    @end
    

    Any constraint with a value of 1 can be set to class NSLayoutConstraintHairline to make the constant effectively 1px instead of 1pt.

    If you ever decide to change the constant to another value, it will just work as any other constraint.

    0 讨论(0)
  • 2020-12-04 11:16

    In Swift:

    @IBOutlet var hairlineConstraint: NSLayoutConstraint! {
        didSet {
            hairlineConstraint.constant = 1 / UIScreen.mainScreen().scale
        }
    }
    
    0 讨论(0)
  • 2020-12-04 11:19

    Just in case someone else comes here wanting to know how it can be done programmatically, heres how you do it:

    Interface Builder

    Make a height constraint in IB to the desired view and set the constant to 1.

    enter image description here

    Then you will need to CTRL+Drag from the constraint into your custom view or ViewController.

    Whenever the Xib is loaded, be it in awakeFromNib or viewDidLoad, you are going to set the constant of the constraint to the scale of the display:

    Swift

    onePixelViewHeightConstraint.constant = 1/UIScreen.main.scale//enforces it to be a true 1 pixel line
    

    Objective-C

    self.onePixelViewHeightConstraint.constant = 1.f/[UIScreen mainScreen].scale;//enforces it to be a true 1 pixel line
    

    Enjoy

    0 讨论(0)
  • 2020-12-04 11:21

    Add a 0.5 pixel height constraint via Interface Builder:

    Xcode 7.3 right down corner: add Height constraint in Pin.

    0 讨论(0)
  • 2020-12-04 11:23

    EDIT: This answer was for @2x only devices. At that time @3x was not yet on the market!

    Nowadays, @mdvs's answer seems to be the cleanest.


    Making 1px line in IB is hard even for retina-only devices. Finally I've achieved it using the User Defined Runtime Attributes.

    Here's a screenshot for setting the Height Constraint to 0.5 px (which is 1px on retina devices).

    0 讨论(0)
  • 2020-12-04 11:27

    With Xcode 6 and introduction of @IBInspectable and @IBDesignable keywords it is definitely possible and rather simple. No need to subclass/outlet anything.

    You can do an extension (category) for NSLayoutConstraint with following code (swift):

    extension NSLayoutConstraint {
    
        @IBInspectable var preciseConstant: Int {
            get {
                return Int(constant * UIScreen.mainScreen().scale)
            }
            set {
                constant = CGFloat(newValue) / UIScreen.mainScreen().scale
            }
        }
    }
    

    Then you can choose needed constraint in your IB.

    IB objects navigator part

    Go to its properties and set the value.

    IB properties of NSLayoutConstraint

    In above case it will render a view with 1 px height on 1x, 2x and 3x devices.

    0 讨论(0)
提交回复
热议问题