AutoLayout has buttons on top of each other - ios

I have constraints on my UIButtons in iPad app so that the top row of buttons stays a certain distance from the UIImageView, and so the bottom row of buttons stays pinned fairly close to the bottom. However, when I rotate it goes all wrong. Here is the image of when in Portrait and when in Landscape. How can this be fixed, given the less amount of vertical real estate in landscape mode?

You will either need to scale the size of your subviews upon rotation, or use a scroll-view.
Because you are positioning your buttons a certain distance from the bottom of the screen instead of keeping consistent spacing, they overlap when not enough space is available. (I'm assuming from your screenshots that it is breaking the constraint between the 2 rows of buttons, assuming there is one)
You can relate the available space for each row of buttons to the total vertical size of the main view. For instance, let the image be 50% of the total vertical space, and 25% to each row of buttons (I would set up a container view for each row and then add constraints to the containers). Make sure that your images/buttons properly scale and maintain their aspect ratio.
Another option could be to recognize when the view rotates, and modify all of the constraints so that your buttons all layout in a single row, but this could involve a decent amount of coding to swap/change most of your constraints.

Related

How can I center four buttons horizontally and vertically?

I would like to center four buttons horizontally and vertically. Each of the buttons shall get a background image later, so I think it's important that the aspect ratio stays more or less the same, otherwise the pictures won't fit onto the button.
A screenshot of what it should look like:
All buttons have the same size and should take up as much space as available (small margin, 10px or so, not more if possible). I don't know how to set the constraints that the application looks good on all devices (iPhone 5s and iPad 10.5").
The application must not necessarily run in all orientation modes.
How do I have to set the constraints?
This problem can easily be reduced to the problem of making a maximum square centered in its superview. You can do that easily with just four constraints:
The rest — the grid of buttons — just falls into your lap. For example, make my blue view a UIStackView containing a two UIStackViews, and there's your grid. Or you could do it with explicit constraints yourself.
If this app can rotate, you might need two sets of constraints, one for when the width is less than the height, the other for when the height is less than the width. You would then have to swap them in code. But that's another issue, and is not difficult.
Add them to UIStackView, for placing them horisontally use horisontal value for axis property you can customize it with distribution, alignment and other properties of UIStackView.
It's the easiest way

iOS fixed size views displaying different widths and heights

So I have some one line views that are set to be dividers between fields. They are UIViews that I just set the background color on. All of them are in IB with constraints, the width or height set to 1, and then tied to other fields and/or the leading/trialing space to the superview.
The view size for he VC is inferred, and is at the 4 inch size.
But when viewed on a 6 device, or in the simulator, these single point views sometimes come out as what appears to be 2 points. I've tried raising the compression resistance of the views, as well as tying them to items that don't require them to expand if it's needed to display the view while keeping to the constraints.
The top, middle and bottom lines, as well as the two vertical ones are all 1 point width or height views.
What is causing these views to display like this?

Really struggling with Autolayout - 4 buttons arranged 2x2

I have a fairly simple scene in my Storyboard, but I can't manage to arrange the four orange buttons with Autolayout.
I've been trying to get this right for a week. I've searched online and I must not have the right keywords, because I haven't found anything that applies.
Everything but the orange buttons are behaving correctly for all devices.
The orange buttons should be arranged in a 2x2 grid.
I want the distance between the "Question" label and the first row of buttons to be the same distance between the second row of buttons and the "Home" button.
I want the orange buttons to retain the same aspect ratio, and I want them to grow/shrink as much as possible, but they should remain centered horizontally and vertically between "Question" and "Home" and the left/right edges.
Use container views to divide and conquer the layout.
Start with top, middle, and bottom views:
The middle view constraints are:
Center X in container
Center Y in container
Aspect ratio 1:1
Leading/Trailing/Top/Bottom space = 10, 250 priority
Leading/Trailing/Top/Bottom space >= 10, 1000 priority
This will give you support for both landscape and portrait orientations:
Now you can add the 4 grid views inside of the middle view:
The grid views will have equal widths and heights to each other, and set the spacing between the views to taste.
You can only do so much using the storyboard. You will either have to insert an outlet and manually manage the frame sizes, or you can programmatically use autolayout to mimic your intended behavior. I can help you with the programmatic part if you are open for it.

Adjusting button width and height according to device

I am attempting to create a keypad for an app that I'm playing with. The keypad will be a standard four rows with the numbers 0 - 1, a decimal, and +/-. Nothing special. I am attempting to get it to only take up 1/2 of vertical screen space. Using constraints, I pinned the bottom left button to the left and bottom of the screen, and the bottom right button to the bottom and right of the screen. I set the width and height of the left button, and then used Aspect Ratio on that button. On the remaining bottom buttons, I control-drag left to the one next to it, and select Horizontal Spacing, Center Y, Equal Width, and Equal Height I then control drag from each of the buttons above, to the button below, and select Vertical Spacing, Center X, Equal Width, and Equal Height from the list of constraints. My thinking was that the bottom left button would adjust based on the device selected, and then all of the other buttons would adjust based on this button.
Apparently, I was wrong. The buttons are nicely set up in that they remain above and next to one another, without any spaces, but they are not taking up exactly one half of the screen. Depending on the device selected, there is proportionatally more or less space above them. Apparently, I am either missing something, or I am totally approaching this the wrong way.
Any advice on how to accomplish this task would be appreciated. I'm still learning about how constraints play with one another, but I thought that my idea was sound. Make one button adjust itself to the correct size (Aspect Ratio on the bottom left button), and then force all of the other buttons to maintain the same width, height, and distance.
The bottom left button is not going to adjust its size, because you gave it a fixed width and height. The buttons should not have any explicit widths or heights set on them. They should all be set to the same size, and as long as you have constraints going from the left edge to the right, that should set theirs widths. Their heights will also be set by virtue of the aspect ratio constraints.
This will cause the buttons to just their size based on the screen size, but it doesn't make them be half the height of the screen (that could only work for one screen size with a particular aspect ratio). To make the buttons take up half the vertical height of the screen, you would probably want to enclose them in a view that was that height. However, then you'll have a conflict between the aspect ratio of the buttons, and the aspect ratio of the screen. You either need to give up on the aspect ratio, or, not pin the right side of the buttons to the right side of the screen, and use an aspect ratio that will make the buttons all fit on the screen.

How to shrink height of UILabels if needed via Auto Layout

I have several UILabels laid out on my View Controller. While working in Portrait mode, I have increased the height of each label so that the boundaries are all touching - they're stacked on top of each other without any spacing in-between. I have a big title and button located at the top and bottom. I have already fixed the top title to always be centered at the top, and the bottom button fixed to the bottom. I also have fixed the middle label to be vertically and horizontally centered in the view. That's all working great. My problem is, I can't seem to figure out how to get the labels to all fit on screen and mostly vertical centered collectively when the height is reduced. I would like it to reduce the height of each label as needed (but not too short such that the text is clipped). I've tried pinning the heights and then changing them to less than or equal to, but in doing so it wants to update the frame to remove the extra height, causing the buttons to no longer be stacked right on top of each other. Then if I set vertical spacing constraints, it will be fixed so it will result in the labels too close together in Portrait if I use the standard value, or if I go with a fixed number they won't adjust to fit in Landscape.
What do I need to do in order to have the labels expanded to fill most of the view when in Portrait, shrink a bit on a 3.5" screen, then really shrink when in Landscape? The labels should all be centered collectively, based off that middle label that's always centered. This is basically what I'd like to obtain:
What you want to do is set the label's heights to be equal to one another. Then provide constraints for distances between each label, and between L/R sides and top/bottom sides. This way, the labels will shrink when the screen height shrinks.

Resources