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

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

    You can do it in .xib file. Just edit it as text and set constant="0.5" instead of "1" in your view height constraint

    <constraints>
    <constraint firstAttribute="height" constant="0.5" id="xUN-dm-ggj"/>
    </constraints>
    

    In Interface Builder

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

    Seems that it is not possible, one must do it programmatically or build a custom view.

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

    Based on @Nevs12 's answer and it's comments, I think it makes more sense to use such thing:

    extension NSLayoutConstraint {
        @IBInspectable var usePixels: Bool {
            get {
                return false // default Value
            }
            set {
                if newValue {
                    constant = constant / UIScreen.mainScreen().scale
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 11:34

    I created NSLayoutConstraint subclass:

    class HairlineConstraint: NSLayoutConstraint {
        override func awakeFromNib() {
            super.awakeFromNib()
    
            self.constant = 1.0 / UIScreen.main.scale
        }
    }
    

    Then simply create your view in interface builder, add height constraint

    and set its class to HairlineConstraint.

    Done.

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