I want to add constraints to side by side four buttons. I've tried many methods on Stackoverflow and different sites, but the result is always like the picture. No problem with screens like the iPhone 11 Pro Max. Problems occur on small screens like iPhone 8.
This is xcode design screenshot (iPhone 11 Pro Max)
This is iPhone 8 screenshot
I selected them all and gave the top, right, left, height, equal width, equal height constraints.
I selected all of them and added top, right, left and height constraints. When I add Stackview, the third row gets corrupted.
Related
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()
I wasted few hours on constraints and I am still having issue.In
iPhone 6 screen, it is working perfectly but it is not working in iPhone 5/4. I set all views (image is background) to have fix W/H and hit auto add missing constraints.
As you can see on iPhone 5 its always pin to right, sometime to left. I Can't add anything its comes with error.
Any help? or some damn good tutorial for this :)
I made an example just for one row of your buttons, so they can be reused for other UI elements.
I put these constraints onto my buttons:
LEFT BUTTON:
left leading constraint
top leading constraint
height constraint
RIGHT BUTTON
right trailing constraint
top leading constraint
height constraint
And constraints related to both buttons:
(you need to select both buttons to be able to apply relation constraints)
spacing constraint between buttons
equal width of these buttons
In Interface Builder it looks like this:
And these layout is universal, so I chose some device sizes to present:
iPhone 5
iPhone 6
iPhone 6 Plus
... etc.
This is my first post. I just started coding. I want to make my first simple app. I own an iPhone 5S and when I add constrains in compact width / compact width & regular height(in XCode), it shows it right on the iPhone 6S plus simulator. When I try it on my iPhone it just looks weird. I know the sizes of the iPhones are different, but how can I correct this? Now, if I code in compact width / compact width & regular height, it feels like I am coding for an iPhone 6...
I hope someone can help me.
Here is the Storyboard:
I use compact/regular size classes for portrait, and any/compact for landscape, for those two phones you mention (actually, for all of the phones).
And, (squinting at your constraints in the tiny pic), it looks to me like the constraints are "wrong" (not optimal, at least). You've got the button pinned 550 down and 280 across from the edges, which won't look right for smaller devices.
Try aligning the button to horiz and vertical centers, and it will look right everywhere.
Or pin ONE of the edges of the button, and ONE of the top/bottom edges of the button, to the layout margins on one corner. Then it will look right on all devices.
Instead of removing the trailing and bottom constraints, as #Kevin suggests, set both of their constants to 0 (or whatever's reasonable), and make them greater than or equal, not equal.
For the constraints to appear fine for different screen sizes, one possible way is to add the required constraints to your button or label or any other component and have a preview look at it in the assistant editor section.(Click on assistant editor and in the split screen that comes up, find the Automatic section and within it the preview option)This helps to view the layout of your app for different screen sizes. This helps to manage your auto layout issues by a good extent.
I'm new to iOS development, so setting constraints for objects on the storyboard in Xcode has been difficult for me.
As this picture below shows, I have 3 borders for buttons I created in sketch so they are all images, each of them inside of a image view:
I tried adding constraints by pinning all three images to the left, right and below themselves but I got errors for it and it did not come out right at all on the simulators. All of the images are in aspect fill mode, and I have the view in the size of an iPhone 4 when I put the images in the storyboard. Here's an example of what it looks like in the iPhone 6 simulator without any constraints:
What are the constraints I need so that it looks okay for all iPhone and iPad sizes and looks like it does in the storyboard? And landscape mode will not be a part of this application, so that will not need to be accounted for.
You can also use button 1 equal width constraint
and button 2 equal width constraint
For more details http://www.raywenderlich.com/83129/beginning-auto-layout-tutorial-swift-part-1
Which is your requirement???
I have 7 labels on a cell in TableView in Landscape mode and I have set the Top Space and Leading Space on each. My issue is it is working fine with iPhone 5 screen it fit well but I get and empty space with iPhone 6 and 6 Plus screen on the right place. How I can fix that with the constraint?
Example:
iPhone 5:
iPhone 6
Please check this youtube link https://www.youtube.com/watch?v=WTMpJJ9Ofm8
In this link it is showing how to arrange vertical spacing.
You can apply same logic to "Trailing Space to Container Margin" for equal spacing. By changing the multiplier value of the constraint you can change the spacing.