Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 programmatically, but can I do it in the Interface Builder? For example I cannot set a UIView to have a height of less than 1.

If I can do it in IB then I don't have to declare an outlet and manually set the frame in awakeFromNib.

like image 997
i_am_jorf Avatar asked May 14 '14 22:05

i_am_jorf


People also ask

How do you add a line in storyboard?

You can also use a UILabel, simply set the constraints to the leading and trailing edges of the view controller, set the height to whatever size you want your line to be, set the text color to transparent and the background to the color you want the line to be, finally set the top constrain to the bottom of what you ...


1 Answers

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

like image 193
Daniel Galasko Avatar answered Oct 04 '22 10:10

Daniel Galasko