Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

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.)

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top