4

I want to to assign a float value to a UIButtons frame in Storyboard. enter image description here

But I can only use int values. Is it discouraged to use .5 for example? because I have some buttons and I want to place them according to the original photoshop design.

for example I have a button I need to be placed at X:151 , how can I achieve that?

Eli Braginskiy
  • 2,867
  • 5
  • 31
  • 46

3 Answers3

4

Do NOT use a float value. As you have rightly observed, Interface Builder prevents this; but you should not do it even in code. Just the opposite: when you assign a frame (or related component) in code, you should set it to an integral value first. (In fact, there are even functions such as CGRectIntegral to help you.)

The reason is that otherwise you can end up between pixels on the screen, and the view will not display correctly (because there is no such thing as half a pixel). Stick to whole numbers of points so that you are using whole numbers of pixels.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Ok thats exactly what I thought. I never do that but was wondering if I'm just have lack of some knowledge. Can you add some apple guidelines on the subject since I didn't find any so I could accept this as correct answer? – Eli Braginskiy Feb 22 '14 at 18:36
4

Do it programmatically (click here for example), I don't know the reason why storyboard prevents from using non-integer values for frame ingredients, however I do know three things:

  1. It works programmatically. (just assign a dummy value in storyboard and overwrite it in code later on.)
  2. Apple uses non-integer values for their native objects in iOS, for example the line separator between cells in table view is 0.5 point height.
  3. Points are not pixels (response to the accepted answer, according to the time of writing these lines.)
Community
  • 1
  • 1
Yizhar
  • 875
  • 9
  • 11
  • My answer does not confuse points with pixels. It makes the distinction exactly. – matt May 05 '15 at 15:13
  • You are right in the sense that you're not confuse between them, however you've mentioned non-integer points would lead to fractional pixels, which make an unbased connection between fractional points and the resulting pixels. (if you disagree, then you'll have to point to a reference in Apple documentation that forbid using fractional points, and contradict their own usage of 0.5 points as I've mentioned the table cell separator height.) – Yizhar May 06 '15 at 16:49
  • "and contradict their own usage of 0.5 points as I've mentioned the table cell separator height" But that is because on a double-resolution screen 0.5 points is 1 pixel - an integer! – matt May 06 '15 at 17:01
  • 1
    So you claim that using 0.5 point is safe or unsafe? In your (accepted) answer you said "Do NOT use a float value." Apparently Apple engineers didn't took into account your accepted answer and used 0.5 point in their own iOS code. You didn't provide a proof or reference to your claim, and you didn't contradicted the fact that iOS use 0.5 point in their popular objects. – Yizhar May 07 '15 at 17:43
0

First of all I was not able to find a solid reference of Not using float value on storyboard. So following a workflow/procedure to achieve float value on storyboard. Last tried with Xcode Version 7.3.1 (7D1014).

Select the view. Remove any previous constraint for the UIView if any. Then add all the constraints including the floating value. On my case it was Leading, Trailing, bottom and Height. Only Bottom have a Int value others are float. Press the "Add X Constraints" button.

enter image description here

Finally I have the following:

enter image description here

The problem is that whenever I want to edit the value, I have to remove all the previous constraints and add them again according to the new constraints.

Shad
  • 1,587
  • 2
  • 20
  • 29