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.
Related
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.
This should be incredibly simple but somehow has proven incredibly complicated for me with auto layout.
I have a horizontal bar shaped button. I want to center a round information icon below it. Auto layout keeps trying to stretch the round button into an oval and is not centering it from left to right as in the following:
The above is the result of the following constraints that should work but don't.
Can anyone please tell me the correct constraints to add to accomplish this simple task.
Thank you.
If the image is being stretched, then set its contentMode to be aspectFit or aspectFill.
Once that is done, you can add a constraint to the image so that it and the login button share vertical centers.
Can you see this yellow box showing in your view? It's showing a warning that means the view which is selected right now should be placed here according to the defined constraints. I guess the constraints of this view are conflicting with other views. Try to resolve this warning by adding "suggested" constraints or by fixing misplacement. I hope it'll work, if not then give me your code I'll resolve that issue for you.
Im having a frustrating problem as I've worked with constraints in the past and have simply used the "Add missing constraints" to do the job, however I am having a LOT of difficulty achieving something very simple -
I have a larger button and a label positioned on my xib file. I need both these centered horizontally and scale with the device, and more or less maintain the distance between each other. Basically just need them to look reasonably good and for the button (which has an image within it) to scale but maintain aspect ratio.
Add missing constraints has not worked in the least as this results in neither the button nor label scaling (both stay small) and the space between them being too big on larger screens. I've tried every other constraint configuration but the button just ends up being squished or going off the screen. Here is what I'm trying to achieve:
The troublemaker is the button. Is there a way to do this programmatically? I am desperate here. How can I configure the constraints?
How do you set a distance between 2 objects?
Center the button horizontally and vertically with the view. Center the label to the view and give vertical spacing from button to the label. Also give width constraint to label if necessary. This will give you the desired effect.
For UIButton:
1.Centre Horizontally & centre vertically
2.Give Aspect ration (or) use leading,trailing,top,button for scale it to different device.
For UILabel
1.Centre it horizontally.
2.Set vertical to the UIButton.
2.Set Aspect ratio.
Hope this helps you.
I found another stackoverflow post asking about essentially the exact same thing I am trying to solve (iOS Autolayout how to stretch 2 button horizontally), however I am still having issues figuring out how to go about aligning the edge of my object to the center of the superview. I need my two buttons to be aligned side-by-side just as in the example in the post I linked, but I cannot figure out how to reference the center of the superview to set my alignment to it, like is shown in the screenshots. Could someone provide a little more insight as to how to achieve the result mentioned in the linked post? Thank you so much.
One way is to set the pin the left button to the left edge. Right button to the right edge, Pin the left side of the right button to the right side of the left button. Then set their widths equal. This is the easiest way for me if I'm using storyboard.
Make sure the constraint between the two buttons has a constant of 0 or however much space you want between them.
If you take a look at this screenshot it will click that button when its red or yellow it will tell you what constraints you are missing on a button.
If a view is misplaced simply hit the view and press cmd+option+"=" and it will give you a preview based on the constraints you've placed on it.
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.