I am trying to learn auto-layout/constraints but for the life of me I cannot figure out a solution to my problem. I have multiple buttons surrounding a 'main' button. I would like the layout to remain the same and the buttons to auto resize depending on the device AND to remain in the center of the superview. I have been reading and trying and have yet to get anywhere. Any help would be greatly appreciated.
At the moment I am not worried about landscape view but if anyone would like to elaborate that would be great.
Any references I could read or videos would be excellent as well.
Thank you kindly
Example Layout:
I can offer some advice.
Firstly, all your views and buttons should have proportional widths and heights relative to the screen size of the device you're using so it will look decent across different devices.
Secondly, apart from the width and height constraints, your middle "main" button will also have a centerX to your UIViewControllers's view as well as a center Y so you can keep it pinned to the middle. This is important because the other 6 buttons will be pinned relative to this main button.
Let's start with the other buttons, specifically the one above the main button and the one below the main button. These two buttons also have width and height constraints. Now, horizontally, you have two options - since these guys are always in the middle of the screen, you can align their centerX's to the main button, or to the UIViewController's view itself - it doesn't matter as long as it's in the middle. Lastly, we have to consider the vertical distance from the main button. These distances should be proportional to the total height of the screen so it will look good across all devices. You'll be adding a constraint from the bottom of the top button to the top of the middle button. For the bottom button, you'll be adding a constraint from the top of the bottom button to the bottom of the middle button.
Now for the other 4 buttons, apart from the height and width constraints that they will have, you should constrain the horizontal constraints to the top and bottom middle buttons, and then constrain vertically to the main button.
Related
I'm trying to make a row of buttons on the bottom of the screen and the buttons are images. I was having aspect ratio issues so a few sites recommended making image views and simply put transparent buttons on top of them. I've placed six image views in a stack view and constrained it to the bottom. I thought it would make sense to put six buttons in another stack view and then also constrain it to the bottom, but it always constrains to the nearest neighbor so now the images are actually on the bottom of the screen, but the transparent buttons are vertically sitting above them. Is it possible to have both constrained to the bottom of the screen so that they completely overlap? Thanks!
It is very much possible, from what you have written I assume that your second stackView is pinned to the top of First stackView. Change that constraint to bottom of your superView. This would be enough for your requirement.
Also Instead of using two different stackViews, I would recommend doing this in one single StackView, which will 6 UIViews inside and each UIView will have UIImageView and UIButton inside.
(Thanks, Sujith Chandran and Neil Hiddink for pointing the way!!)
Here is my solution: After selecting the bottom constraint for the stack view that contained the transparent button overlay, I went to the size inspector and changed the "Second Item" to superview, opened second item again and selected bottom, and then set Constant to 0. Eventually I had to change the value of constant to -70 to get the desired position because I previously set the button images to a height of 70.
XCode 9.3: macOS 10.13.4
I have an swift app where the buttons are off-kilter. I need them at the bottom of the screen and centered. The iOS simulator for iPhone 8 shows the buttons correctly. However, the simulator for the iPhone SE and the actual SE (my iPhone) show the buttons anchored to the right edge of the screen. How can I insure that the buttons are displayed correctly on all iPhones?
First sol
this layout can be done correctly if the center of the middle button is equal to the center of the screen
Second sol
create a UIStackView with these constraints
1- left , right , bottom to screen
2- height say 100
with distribution set to fillEqually , spacing set to say 20
drag the buttons inside it and they will look right in any device
Remove the constraints from the 3 buttons: Calc, Clear, and Help
Align clear to be horizontally centered
Align Calc trailing offset from Clear's leading.
Align Helps Leading offset from Clear's trailing.
Align Clear bottom offset from the bottom margin.
Align Clear, Calc, and Help along to center vertically with one another.
You have two choices: 1) use the legacy struts and springs mode in interface builder to distribute your items as you need them or 2) spend the time learning auto layout, and use it instead. There isn't any easy answer because of all the different screen sizes, and you haven't even considered iPad screens here. The best way to pick up auto layout is to view the WWDC videos on the subject. Yes, it will take longer, but your app will respond better to screen changes, and you will also be able to inherently accommodate new screen sizes.
Use a horizontal stack view for buttons, and set spacing between buttons.
Approach 1 - Set trailing space and leading space, pin the view to bottom and set a fixed height for stack view.
Approach 2 -
Set fixed height and width to stack view and center horizontally, and pin the stack view to bottom.
Always try to use stack view, with that you will reduce the number of autolayout issues you run into.
Please add the constraint for stack view and adjust like that:
I hope someone can help here as I've been reading and watching a lot about size classes as I've just started work on first app using size classes.I thought I'd got the idea but having a slight issue.
I've got a view with 10 buttons in it. I want the buttons to re-size to fill the desired screen but as you can see from the screenshot below all I'm left with is loads of white space on the bigger views?
I've set constraints to the view and to each other so I though Aspect Ratio should solve it....No, all I get them is one huge buttons and the rest stay the same!
I've researched a lot so any guidance would be greatly appreciated.
Please see this image of the issue.
Thanks
Pin the top of the top button to its superview.
Pin the bottom of the bottom button to its superview.
Assign a fixed vertical space between each button.
Select all the buttons and specify "Equal Height."
This should take care of your vertical constraints, and the button heights will equally expand or contract, depending on the screen height.
Here is design of my storyboard:
And here is output:
Why this button is not located in its correct location?
This is a common question on Stack Overflow. You need to add auto layout constraints to the button. It doesn't know if you mean "in the centre" or "300 points from the left". You can add them by selecting the button and tapping the button with the square on it in the bottom right of the editor.
These are the buttons.
The buttons are...
Align views. Either multiple views or along a single view with its superview.
Add constraints to a specific view. Left, Top, Height, Width, etc...
Auto constraints. Update frames to constraints. Etc...
Resizing options.
You need to add constraints to ALL of the views in your app.
So, on button2 you will need to tell it...
How far down from the top.
Align the button to the centre of the superview.
For button1 you will need...
How for down from the top.
How far in from the left.
For the map...
Zero from the bottom, left, and right. (That's three separate constraints)
How much spacing between button1 and the top of the map.
I’m having bit of a tough time getting my head around auto layout, constraints, priorities, compression and content hugging.
I think I understand it but getting it to play nicely seems to be impossible.
I have this simple view, with 3 buttons and a label.
I want them to be able to adjust to fit the screen when the user orients the screen.
All the buttons are pinned to the leading and trailing superview.
The top label’s top is pinned to the super view. The bottom button’s bottom is pinned to the superview.
There are vertical space constraints between the buttons.
I have tried varying priority levels for the compression of the buttons to get them to squeeze vertically for the horizontal view, but everything disappears off the screen.
Or one of the lower buttons appears above the upper buttons.
I am sure I am doing something quite simple to make these errors.
Would someone be able to offer me their 2 cents on how to fix this?
Many thanks for any help!
Merry xmas!
Adam
UPDATE - after "update all frames"
It seems button 01 disappears when the view is rotated
**UPDATE #02 - almost working but not sure how **
Guys I seem to have managed to get it to work, but i'm not really sure how.
And it's not reliable - if I adjust the constraints between the label and the first button then the oriented view ends up scaling off out of the bounds of the screen.
I would assume that I could adjust that vertical constraint to be able to change that space, but it just makes a mess.
Pin the bottom button to the view controller's bottomLayoutGuide, not the superview. Do this by control-dragging from the bottom button to the Bottom Layout Guide in the scene's Document Outline.
Also, your interface objects are misaligned; that is, the frames don't match the constraints (or the constraints don't match the frames). That's why you see the dashed rectangular outlines. Go to the floating tool bar in the lower-right corner of the storyboard canvas and click on the "Resolve Auto Layout Issues". From there, either select "Update All Frames…" or "Update All Constraints".
I think the default values for compression resistance and content hugging priorities are adequate.