16

I am new to iOS storyboard, can anyone tell me how to create a line with 0.5 px width? I have read this post:How to create a line width of 0.5 pixels and I know how to make it by coding, but does it have a easier way such as using storyboard?

Community
  • 1
  • 1
user2077625
  • 171
  • 1
  • 4
  • hey @user2077625, did my answer help or do you need further explanation? :) – nburk Apr 14 '15 at 19:12
  • Possible duplicate of [How do I create a 1px line in Interface Builder?](http://stackoverflow.com/questions/23666209/how-do-i-create-a-1px-line-in-interface-builder) – Richard Venable Sep 30 '15 at 21:12

6 Answers6

41

It is working with xib/Storyboard directly.

To make it work: Do NOT hit the return key after entering 0.5 in the Size inspector. Just click somewhere on the storyboard. Then 0.5 will stay as value. It seems that IB checks that value and corrects it only if you hit enter.

The size inspector will still show "0" as value, when you view the constraint again. But in the document outline you can see the "0.5". And it also renders it as 0.5 at runtime.

The constraint as seen in the document outline

beseder
  • 1,352
  • 2
  • 15
  • 25
  • 6
    Who would expect that you must NOT press the **return** key to pass `0.5` to `constant` of a constraint in Xcode 8. Awkward indeed ... – Jakub Truhlář Mar 12 '17 at 23:56
  • LOL we are now in xCode 14.2, and this is still an issue! Not pressing return is still a solution – MQoder Feb 20 '23 at 11:25
7

It can be done in Storyboard itself - just select the desired constraint and go to the "Identity Inspector", now add a new Key Path with name "constant" and set its type to "Number" with a value of 0.5.

PinkeshGjr
  • 8,460
  • 5
  • 41
  • 56
user1863617
  • 129
  • 1
  • 7
7

Just use user defined runtime attributes

user defined runtime attributes

Select your and Constraint and set to User Defined Runtime Attributes : constant Number 0,5

It's simple and safest!

Community
  • 1
  • 1
Daniel Smith
  • 720
  • 7
  • 13
0

You can just çreate a UIView and set its height or width (depending on whether the line should be vertical or horizontal) constraint to 0.5 using AutoLayout.

If you're not using AutoLayout, it's even simpler and you can just set the view's height or width directly to 0.5.

nburk
  • 22,409
  • 18
  • 87
  • 132
0

my solution:

  1. create a view, set a constraint the view: "height = 0px", go to the "Identity Inspector" and edit the priority of this constraint to 750.

  2. then select the view and click the "pin" button, add another constraint to this view: "height = 0.5" with priority of 1000. in the "Identity Inspector" you can see the second constraint automatically becomes "height >= 0.5"

works in Xcode 7.3.1

bubuStar
  • 563
  • 1
  • 3
  • 8
0

If you want a one pixel width or height divider across all different devices (e.g. 2x and 3x screens) you need to account for screen scale. You can do this by connecting your width constraint to an IBOutlet and then setting the constraint constant programmatically.

Define an IBOutlet variable for your constraint in your view controller

@IBOutlet var bottomDividerHeightConstraint: NSLayoutConstraint!

Right click your width constraint in IB and drag and connect it to your newly defined constraint

enter image description here

enter image description here

Then in your viewDidLoad() place the following code

self.bottomDividerHeightConstraint.constant = 1/UIScreen.main.scale
Josh Bernfeld
  • 4,246
  • 2
  • 32
  • 35