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

后端 未结 10 1788
独厮守ぢ
独厮守ぢ 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: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.

提交回复
热议问题