Adjusting button width and height according to device - ios

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.

Related

Constraints to resize buttons to fit any screen - Xcode swift

I don't really understand constraints and have tried many different suggestions found online. All they seem to do is bunch everything up on top of one another or do nothing at all.
I have the following IPad application but I want it to work on any size device, mainly a IPod touch.
The page is simply two buttons that I want to remain the same no matter what screen they're on.
Any help on this appreciated.
It helps to think about points of reference that won't change with different screen sizes. Sometimes you want things on, say the top left corner so you just do constraints to the top and the left.
I'll give you two suggestions
Suggestion One
For your case, it seems like you might want to do constraints off centerY since you want them to be in the middle despite the screen size.
So I would make a constraint to "Center Vertically in Container" and then tap on the constraint and adjust it's value to negative or positive, so that way it's always X pixels above or below the centerY.
Now that's not going to be enough. it knows it's Y position but it doesn't know its height, width, or X position. So you need to add enough constraints to satisfy those.
A few examples:
X/Width: Two constraints to leading and trailing on each button OR Center horizontally and fixed width constraint. (again be careful with fixed width constraints since screen sizes can change, sometimes it's what you want though)
Height: Yeah just give it a height constraint in this case.
Note that this means no matter the screen size they'll always have the same gap between them (and maybe different gaps to the other edges).
Suggestion Two
Use a container view, either a stack view (fill, equal spacing, vertical alignment, a spacing value for gap between) or normal view.
You can make the view a fixed height based off the height and spacing between the buttons you want. Then simply center that container view horizontally and vertically on the super view.
Nonsuggestion
There are certainly other ways (like using buffer views with equal heights constraints. So you'd have an invisible view on top, a view in between and a view on bottom. and you'd give those equal heights constraints and align the buttons to the edges of the invisible views surrounding them. As long as you gave the buttons a fixed height this would work for vertical constraints) but I think these two would probably be the best.

How do you make AutoLayout make UIimageviews bigger according to screen size?

So I have an app I'm working on, but I hit a major problem. In this app we have several images. I'm learning AutoLayout, and I can make stuff appear the same on all device screens, except for the fact that the images (and for that matter, everything) remain the same size. I want them to get bigger and smaller according to screen size. I've tried everything I know. Help!
Here you can use propotional width and height to the superView. At first Ctrl drag from your imageView to superView and set Equal Height and Equal Width. And now set the Propotional height and width to the superView from the constraint list. Here you can set your height and width with Propotional to superView with Multiplier option. And finally set your x and y position of your image Final Image. Here I have used horizontaly and verticaly centered.
Using the image here as an example:
First, place your imageview where you would like it, in terms of how far it should be from borders of the view or other objects.
Then, click on the small tie-fighter like icon in the bottom to bring up the add new constraints window.
From there, click on the four links show in red inside the red box. These tell XCode that it should be "80 units from the left most object, 80 units from the right most, etc".
Now, when you change to different devices, it will always adhere to those standards.
However, depending upon the size you selected, it may not work under certain conditions such as being rotated to landscape, or in a split view on an iPad. This is where you get into the other size classes, which you access by clicking the text at the bottom of XCode that says "w Any h Any".
One item of note, do not select the image size in width or height in the constraints window seen below. Doing so will constrain the image size, invalidating the margins put in place.

AutoLayout has buttons on top of each other

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.

Button autosize with auto layout in Xcode 6 w/ swift

could anyone of you give me a hint, how to implement autosize of a button with auto-layout?
To be more specific:
My button has 50x50px at iPhone 4s. How to implement this button, that its size increases on iPhone 6 plus screen resolution? Is there a kind of multiplier option?
Edit:
I want, that the button w/ 50x50px on iPhone4s screen is increasing proportionally at iPhone5, iPhone6 display.
Thanks.
BR
Use aspect Ratio. It's all about math. Before you jump into the action, let's figure out how we are calculating the proportion.
So, if you want your button to be 50X50 on a 400X600 frame, how will you find the proportion. Easy- your button's width will be 1/8th of your frame and your button's height will be 1/12th of your frame's height. That's it. Now you have your calculation.
Step By Step Instruction:
First make sure, you have the size class as "wCompact hAny". We should see, something like-
Now we are ready-
Select you ViewController and then select "Stimulated Size" in the inspector. There you will see your Stimulator's width and height. In my case, it is 400X600. You can change it to anything, and calculate your proportionality value accordingly.
Now, we need to position our button. I am just setting it at the middle of the frame. So, hold the "Ctrl" button in the keyboard, click and drag the mouse pointer from the button to the immidiate parent view which will give you a list of options.
From there, when you select the centre horizontally and the centre vertically option your button in the frame should look like this-
Now you can see some orange dots, but ignore that for now.
This is the part, where you will have your answer. To set the button size proportionally, you can use aspect ratio.
Again, hold the "Ctrl" button in the keyboard, click and drag the mouse pointer from the button to the immidiate parent view. This time select aspect ratio from the list.
Select the aspect ratio constrain from the constrain list to edit. You need to make sure that your first Item is Button's width and your second item is superview's width or vice versa. Remember, you want to set the button's width's proportionality value(multiplier) in accordance to the frame's width NOT height.
Now, set the multiplier to 1:8 if your 1st item is your button's width, or 8:1 if your 1st item is superview's width.
Do the 3rd step again. Select aspect ratio again, and select this constrain to edit. However, this time set your first Item as Button's height and your second item as superview's height or vice versa and change the multiplier to 1:12 or 12:1 accordingly.
Great, if you have followed unto this point then you are done. You should see something like above.
You need following three constraint
For X position : Leading Space to SuperView
For Y position : Align Center to Y
For Width : Trailing Space to SuperView
Check ScreenShow

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