How to scale buttons for different devices - ios

I have buttons on my screen I have attached constraints but on different sizes of screen I don't see my buttons right as I want to. There are the same size on different screens and I can't see all buttons on small screens
Here is a good version of it on iphone 8 plus in xcode
and then I change view to iphone se or any another iphone and I get these troubles:
What do I need to do to have the same screens?

Make the buttons have a width that is in proportion to the view
width.
Then add a 1:1 aspect ratio constant so that the buttons remain
square.
Then position the buttons in relation to the screen dimensions (not
absolutely).
This should get the job done for you.
Setting proportional width
Suppose the main view's dimension is 375 x 667 in interface builder. Suppose you place a 90 x 90 button on it. To make it always have a width in proportion to the main view's width,
Add an equal widths constraint to the superview for the button.
Edit the constraint's multiplier: Change it from 1 to (90/375)
Doing the above makes sure that for different screen widths, the button's width will also change in proportion.
Here are screen shots from iPhone XR and iPhone 5s, that I took from a sample I just did to try this out. As you can see, the buttons stay within the screen and maintain their shape. Hope this helps.

You don't seem to have defined the constraints of the button to superview. eg: left edge, top edge...
*edit: post complaint below #raisedeybrow
You will need to define (add constraints) how far from the edge (left and top, right and bottom) you want your button/s to be. Then apply content hugging and compression priorities on buttons for all of them to scale as you'd like. Some will need to be bigger, some you don't want to allow to shrink etc. Lots to set if you'd want it to look fluid

Related

how to set autolayout for different devices by using storyboard

I have an Iceland map which contains 9 different area buttons. Every button has a different size. I want the map can show the same on different devices. I only know to set the central button with horizontally in container and vertically in container. However, other buttons, no matter how I set the constraints, they will be a mess on iphone SE or iPhone Plus. (I use iphone 8 as normal)
Can anyone teach me how to set the constraints for the 8 left buttons? thank you!
The best thing for this is a vertical UIStackView nested with 3 horizontal stacks
MainStackView constraints
centered vertically & horizentally , width & height propotional to screen
then drop 3 stackViews inside it set axis = horizontal and drop 3 Buttons for every inner stack
Note: distribution is fillEqually for all the stacks , spacing = 10
look to this Demo
Something I've done in the past is used a single control as the anchor for all the others.
Basically this means taking the centre button and anchoring it to the middle of the container view (centre horizontally and vertically)
I then constrain the middle/top and bottom controls to it (vertical space and horizontally centred)
From there each control in the row is then constrained against these elements (horizontal spacing and vertically centred)
You might also consider constraining the sizes to the centre control as well, so all the controls share the same size. For me, this means constraining the width of the control to a set value and then applying an aspect ratio to the height. I do this because then I can change the value of the width constraint and ALL the control will change size
Because iPhone 5s, iPhone, iPhone+, iPhone X all share the same "sizing classes", it makes it some what more difficult (as you can't apply traits - but you can do it for iPad and iPhone ;)).
At this point, I would bind the centre controls width constraint (and the height if you need it) to the source code and when the view is loaded, determine the device screen size and make minor adjustments to the constraints values.
If you would prefer a complete "storyboard" solution, you could constraint the width of the centre control to the super view and apply a modifier
Using the "device" templates from the storyboard, layout one a iPhone 5s and iPad Pro
The long and short of it is, you have "options". You might even consider using UIStackView to define the rows, personally I find it easier to constraint the controls to a central anchor point - but that's my experience and your needs might differ

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.

Adjusting Y position of Elements for all Iphone screen size using AutoLayout

I am using an Image view which is placed on top of parent view. Color of parent viewis changed. i want my design to look same on all iphone sceen sizes in portait. top tabel is positioned accuratly as it should be in the center. but I am having issue with my Logo as you can see in screen shots Logo is at perfect place in only iphone 5.5 inch. what constraint i should set to position my logo as same point (lets say S in "YAS" should be touhcing the hair of girl as in Iphone 5.5) for all devices ?
i have applied constaints but its not working please guide me
This is wahtI am getting in previw
These are my Constraints
This is my Xib
I tried differecnt values for multiplier but nothing happened
image View is in Scale to fill mode
Position your LOGO Horizontallycenter manually and then give TOP , LEADING , TRAILING and aspect ration constraint . When your view's width will increase these constraints will increase height of your Logo.
Also set your ImageView(Logo) View -> Mode to Bottom (in Attribute Inspector).

Interface Builder changes my Button dimensions

Using Xcode 7.3, I have a stop button (just a 100x100 pixel image, no text) that in my Any,Any size class I have added a center horizontally constraint and then a trailing constraint. This keeps it centered and just a bit off the bottom edge of the screen - which works for most cases.
However for a 4-inch iPhone in landscape, my other elements constrain things and I want to put the button vertically centered and somewhat off the right hand side of the screen.
When I go to the Compact Width, Compact Height class view, add Center Vertically in Container and then choose Update Frames to see the result, IB changes the height of my button from 50 to 300. Why does it do this? I want to keep the image dimensions the same.

Proportional distance to top in autolayout and device rotation

I have an UIImageView in an iPAd Screen. The three Auto Constrains are
1) centered horizontally in the container
2) fixed width
3) fixed height
I have another constrain which tells the image view to be 100 points under the top of the screen.
If the device goes to landscape mode, I want to have this value 70 (the numbers 100 and 70 are imaginary, can be others, relations, proportions etc.)
What should I do for this without solving it programmatically, pure auto layout in the storyboard interface builder.
Thanks for helps.
Use the multiplier of a constraint, accessible by selecting the constraint in the XIB.

Resources