I don't understand how having together left and right orizontal constraints together.. I still don't understand the exact mechanism beyond interface builder, its constraints and the effective results on the simulator:
I expected the label "ingredienti" in the center of my view (like my input text above with same type of constraints).. Why does it go outside my "screen" in simulation preview?
A leading or trailing constraint (which is what you've set up) sets a fixed distance between the left edge of the view and whatever you've associated it with.
The default simulated size for view controllers in the new universal storyboards and xib files in Xcode is 600x600. But none of the current devices actually have this size.
So, if your label has, let's just say, 100 width, then in order to "center" it using left & right constraints using the simulated interface builder width of 600, we'd create a left constraint of 250 and a right constraint of 250 (250 + 100 + 250 = 600).
But again, none of the actual devices have this width. So if we run your app on an iPhone 6, it'll have a width that translates to 375 "points". If we run it on an iPhone 4s or iPhone 5/5s, it has a width that translates to 320 points.
So, our 100 point wide label is constraint with a constant distance of 250 points from the left edge of the parent view. 250 + 100 = 350. But the iPhone 5 only has a width of 320 points, so part of label appears off the edge of the screen, and the right constraint is broken because the left and right constraints cannot be simultaneously satisfied (you probably have a bunch of warning messages about this in your log when you run the app).
If we want the label centered, we must create a horizontal center constraint (as mikle94's answer demonstrates).
You have to remove left and right constraints from your label and then add "center-horizontally to superview" constraint.
Related
I have buttons on my screen I have attached constraints but on different sizes of screen I don't see my buttons right as I want to. There are the same size on different screens and I can't see all buttons on small screens
Here is a good version of it on iphone 8 plus in xcode
and then I change view to iphone se or any another iphone and I get these troubles:
What do I need to do to have the same screens?
Make the buttons have a width that is in proportion to the view
width.
Then add a 1:1 aspect ratio constant so that the buttons remain
square.
Then position the buttons in relation to the screen dimensions (not
absolutely).
This should get the job done for you.
Setting proportional width
Suppose the main view's dimension is 375 x 667 in interface builder. Suppose you place a 90 x 90 button on it. To make it always have a width in proportion to the main view's width,
Add an equal widths constraint to the superview for the button.
Edit the constraint's multiplier: Change it from 1 to (90/375)
Doing the above makes sure that for different screen widths, the button's width will also change in proportion.
Here are screen shots from iPhone XR and iPhone 5s, that I took from a sample I just did to try this out. As you can see, the buttons stay within the screen and maintain their shape. Hope this helps.
You don't seem to have defined the constraints of the button to superview. eg: left edge, top edge...
*edit: post complaint below #raisedeybrow
You will need to define (add constraints) how far from the edge (left and top, right and bottom) you want your button/s to be. Then apply content hugging and compression priorities on buttons for all of them to scale as you'd like. Some will need to be bigger, some you don't want to allow to shrink etc. Lots to set if you'd want it to look fluid
I am attempting to vary my auto layout constraint by iPhone version. The vary for traits allows you to differ by size classes. However all the iPhones in portrait orientation are all compact width and regular height.
Assuming I have an element that is horizontally centered but I want my distance from the top of my main view to vary based upon device size, how should I accomplish this?
You cannot specify screen-sizes or device versions in auto-layout - only size classes.
If relative positioning/sizing doesn't work for you, and you want to do positioning such as 5 pts on iPhone SE and 15 pts on iPhone 6, you have to do it via code. Your options are:
Don't use auto-layout...
Change constraint values (constants, multipliers, priorities, etc)
If you read through Apple's docs on auto-layout, you can see it is targeted toward adaptive interface design, not alternate interface design.
Method 1 : Programmatically changing Constraint Values
If you want to set different constant top values for iPhone variation, then create a outlet of Top Constraint and change its constant value.
_topConstraint.constant = Conditional code for each device.
Method 2 : Setting Adaptive Constraints
Eg: I have taken a view with fixed height = 100, width = 200 and centred it horizontally.
Now it give a proportionate top space as per the device size, please gear up for some maths.
Suppose, the top space in iPhone5 screen = 36 pt.
Total View Height = view top + view height = 36 + 100 = 136
Total Screen Height = 568 Bottom Ratio = 568/136 = 4.17
So, set a bottom layout constraint and add the multiplier as 4.17.
enter image description here
On iPhone 7 plus and other devices, it changes the top automatically.
i have a question about how to setup constraints in a storyboard with autolayout if I want to guarantee multiscreen support so that all UI-controls are accessible and the UI is not messed up on smaller screen-size devices (IPhone 4s or IPhone SE) when I designing on a bigger screen-size device (IPhone 6). I have a design which is build up on a IPhone 6 (in a sketch-file) which I want to transfer to my storyboard. So in general I want to setup all the view stuff in a storyboard and not via code.
Here are my constraints. The button to the bottom has fixed height of 48 and a leadingMargin to the right and left and a vertical distance of 100 to the bottom. The top button has the equal height to the bottom button and a distance of 28 to the bottom button.
Here is the designed View with the mentioned buttons on an IPhone 6. This is the default size where I want to setup the storyboard cause of the sketch file.
Here is the View on an IPhone 4s
As you can see the buttons are to close to the middle cause of the fixed vertical distance and the fixed height. I mean this is obviously cause of the fixed values. So I made some research about percentage position like mentioned here but is this the correct way? Also other ways looks so complicated to me. How can I prevent fixed values like the height and the vertical distance? Is there a way to set the fixed height of 48 for the IPhone 6 and then scale the button down (for IPhone SE) in regards to the screen size (the same for the bottom vertical distance and other fixed values)?
Can someone give me any advice how I can proceed here?
It is a bit complicated I try to explain it as much as I can.
Note: Auto-layout is just like constructing a building every thing step by step.
Select top button and add these constraints (Leading:8,Trailing:8, And select Aspect Ratio checkbox) + And also add "Horizontal center and Vertical center constraints" select the vertical center constraint and change it's multiplier to 1.5 or increase or decrease it's value accordingly.
There should no error after this only warnings if any.
Select second button and add Top constraint to 8. Select both buttons and add constraints which are highlighted in screen short.
Run and check on different screens. Hope it help.
bottomButton.bottom = superview.bottom * 0.9
OR
Change the bottom constraint by code
bottomConstraint.constant = xx
self.view.layoutIfNeeded()
How would I achieve that a view spans over the device width on an iPhone 6 in portrait mode and on landscape having a maximum width of say 400px and be aligned to the right side?
Can this be achieved by using device classes and / or vary for traits? What I really want, is that the view adapts to the width of the device: if it is wider than 400px (because I rotated the device to landscape mode or because the app runs on an iPhone Plus or iPad) it should no longer span the width of the display but rather have a maximum width of 400px and sit on the right side. Maybe this can only be done by code?
Thanks for any input.
What I really want, is that the view adapts to the width of the
device: if it is wider than 400px (because I rotated the device to
landscape mode or because the app runs on an iPhone Plus or iPad) it
should no longer span the width of the display but rather have a
maximum width of 400px and sit on the right side.
This can be achieved by setting the following constraints to control the width and placement of your view.
Pin the view to the trailing edge of its superview with constant 0 and a priority of 1000.
Pin the view to the leading edge of its superview with a constant of 0 and a priority of 750.
Set a width constraint for the view, and make it Less Than or Equal to 400 with a priority of 1000.
When viewed on an iPhone 6 in portrait, all three constraints are easily satisfied.
When viewed on an iPhone 6 in landscape, constraint 2 (which has the lower priority) is broken to satisfy the other 2 constraints. Auto Layout will do its best to satisfy constraint 2 and will make the view as wide as possible without breaking the width constraint which is 400 units.
You will need other constraints for your view to establish its height and vertical position.
I have an UIImageView in an iPAd Screen. The three Auto Constrains are
1) centered horizontally in the container
2) fixed width
3) fixed height
I have another constrain which tells the image view to be 100 points under the top of the screen.
If the device goes to landscape mode, I want to have this value 70 (the numbers 100 and 70 are imaginary, can be others, relations, proportions etc.)
What should I do for this without solving it programmatically, pure auto layout in the storyboard interface builder.
Thanks for helps.
Use the multiplier of a constraint, accessible by selecting the constraint in the XIB.