Laying Out Login Screen In Xcode - iOS - ios

I have a login layout and I want it to work universally on 4 inch, 4.7 and 5.5 inch screens.
I want everything to be as if the screen was "one" and I just made it all x% larger or bigger to stretch and fit the screen.

Part of the challenge in doing this is that different components are adjusted in different ways to scale with screen size, as well as that there are a few ways to do some of those.
Lets look at a few:
You can use Autoresizing to scale a view automatically with with the size of it's superview/container (so this applies to changing from portrait to landscape). You can find this in the size inspector (if you haven't set constraints). You can see an in depth tutorial on it here.
This let's you set both spacing and internal space based on where you place the view originally.
Another way is setting constraints in relationship to another view with a ratio other than 1:1. For example, you can set a constraint of your button to the superview as equal widths and edit that constraint's ratio/multiplier to 1:2 to make it 50% the size or 1:4 to make it 25% of the width, etc'.
For the fonts you should probably use Autoshrink. You can see a detailed answer about this here.

Related

How to scale buttons for different devices

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

How to control constraint over multi devices in iOS/Storyboard?

I've been working on a project that supports multiple screen sizes for iOS. Thanks to Storyboard and auto layout technology, I could be able to handle some constraint automatically based on device screen size. But in some cases, I couldn't figure out how to deal with auto constraint when I apply constant constraint in Storyboard. For example, lets say I put a button in view controller on Storyboard and set constraints (Tailing/Leading space as 44 and Top space as 50). This button surely expand its width based on device screen size but I also want to change top space proportional to its screen size. Assume base top space 50 is set for iPhone 4 screen size (320/480), then I would like to increase it as device screen height increases. The way I manage this for now is, check screen size and change its constraint constant respectively which is 50 * (568/480) (i.e. I want to keep same ratio). I wonder if there is a way to do this trick on storyboard. I would appreciate any advices.

Auto Layout proportionally Scaling Views

I have already found several questions and answers about this topic but I simply cannot get this to work for me.
The blue rectangle is a UIView which contains the Test Label and Gear image button. The red one is a second UIView.
What I'd like to do is to get the blue bar (which is a UIView) increase in size for larger sized screens, which will result in the scaling of the Label and settings button, while the Red UIView shrink in height so the blue UIView can become larger. So basically the question is, how to proportionally scale the UIViews for different screen sizes.
I have tried using aspect ratio constraints, height constraints (greater than/less than) but I just can't get the desired effect.
Thanks.
Try proportional height and width. You need to make constraints for Equal Heights and/or equal Widths and then you double click on the constraint and change the multiplier for example 0.2 if you wish to make First Item to be 20% of second item in width and 2.0 for 200% (means double) in width. to make it increase or decrease proportionally according to your requirement
You have two ways for do that:
1. Proportional constraints:
but if you set a proportional height for one view means that the view change for every little change in screen size. For example, your view can be a little bigger switching from 4" to 4,7", but does not mean that it is a bad idea.
For use that feature just check the answer by Asadullah Ali :)
2. Use size-classes:
if you using Xcode 6 you can use the size classes and add a constraints just for a screen size; it's not difficult to use, check the Apple Documentation.
For this feature you need to able size classes for your Storyboard:
after that you can set up your view controller like you want and add a rule just for one of the classes:
you can tell to Xcode "ok, if the screen size is an 5,5 at least use this rule".
Remember that for add a constraint for a specific size classes you need first select the size class then add normally your constraint: when a size class is selected (you can check in the size classes control) Xcode add the constraints just for that class.
EDIT 1: If you have some trouble with scaling font & similar you can also use size-classes feature just for scaling a font size in a label for example; you find this option by clicking on this after activated the size classes feature:
I prefer tell you everything I know about your problem but in your case personally I would use the first solution: your view can dynamically change whatever is the screen size.
You need just find the best proportional percentage: you can set maybe around 10%, so the 0.1 multiplier with first item your blue view and for second item the red view.

Using Auto Layout in Interface Builder with complex views

So, here's a tricky one. I'm working on a portrait only app that should be compatible with the iPhone 4s, 5/5s/5c and 6/6 Plus. Unfortunately, I'm having some troubles working with Auto Layout in order to make the user interface fully compatible with the previous listed devices.
Here's a screenshot of how the UI should look like (this is from an iPhone 6 Plus that I'm using as a reference for the other screen sizes):
I have a 338x338 points blueCircle, a 180x180 redCircle, a 118x118 greenCircle, a 84x84 purpleCircle and, finally, a 50 points tall adBanner. The result I would like to achieve is to have an iPhone 6 Plus UI that respects these view sizes and a properly shrunk down UI for all the other ones; in addition to that I will let the users pay to remove the advertisement banner on the bottom so I need to resize the UI accordingly too (set circles vertical spacing to the banner and set its height to 0 maybe?). I messed around with Interface Builder and I came out with a decent - not so precise - result. Take a look at the UI on an iPhone 4s screen:
It looks decent but there's major problem. In Interface Builder I'm working on a Freeform ViewController with the same width of an iPhone 6 Plus (414 points) on which I've added all those circle views with the previously listed frame sizes. To make an example, let's temporarily ignore all the other constraints to concentrate on the aspect ratio ones: on the 338x338 blueCircle I've added an aspect ratio constraint, the same on the 180x180 redCircle. Then I've control-dragged from the redCircle to the blueCircle and clicked again on aspect ratio so that it resizes accordingly to the blueCircle. As I said the result wasn't precise since, if I println() redCircle's frame, the console will say that its width equals to 175.666666666667 (instead of 180) points.
Here's the project so you can check it out yourself. I believe its worth more than a thousand words. I'm not very good at Auto Layout and I'm sure I'm creating too many useless constraints. What could I do to improve my layout?
you need to set height and width of all the circle views as proportional to its parent view.
I set the height of blue view as 0.5 proportional to its parent view and its width will be in aspect ration with itself as 1:1
repeat same for all the other views and apply vertical spacing and horizontal spacing as required between views
this is how it looks on iphone 4
this is how it looks on iphone 6 plus

Xcode 5 / iOS7: Trying to add constraints to resize between iPhone 3.5" and 4" screen

I am struggling to use Xcode 5's constraints to fit content (relatively) between 3.5" and 4" screens. To simplify matters, I am only concerned about portrait at this moment
Here is an example screen of a layout on a 4" screen (the tab controller at bottom isn't shown):
I want to lock the proportions of any images (such as this truck), but allow all labels, buttons etc to squeeze more closely together on the 3.5" screen. As all iPhones are the same width, I think I need to leave the x-constraints as-is, but configure dynamic y-co-ordinates, but would appreciate advice on this.
Many thanks.
You can pin the width and height of your image in your storyboard by selecting the image and then adding the necessary constraints (your numbers will be different):
You can add constraints to automatically change the vertical layout by making the relation by "greater than or equal" or "less than or equal", but, in my experience, it is difficult that the result is exactly what you want, as the autolayout engine will not make the line separations the same.
My suggestion is that once you have the layout for one of the screen sizes, you add outlets for the vertical constraints and then adjust those constraints constant property in code on -viewDidLoad. That way the distribution of space will be exactly what you want.

Resources