IOS Stack View Basics - ios

Learning stack view; what's preventing this horizontal line of 60x60 buttons from a) all showing up on both screens (constraints are set on the trailing side so I'm not sure why it's going past the right edge of the screen), and b) scaling the buttons larger. I'm looking to see all the buttons and scale them proportionately to the screen size while maintaining their individual aspect ratios.
Thanks!

1- Put an “aspect ratio “ constraint in each one of them.
2- Put constraints on stack view “leading” and “trailing”.
3- In the attributes inspector for stack view make all views “Equal “ instead of “Fill”.
These setting will make all of them appear with the same size and according to the screen size they will vary but still all of them with the same aspect ratio.

Related

Laying Out Login Screen In Xcode - 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.

How to scale height of views and postion of the views relative to the screen size using AutoLayout [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Using Autolayout what is the best way to keep a view's relative position and scale to each screen size? For example there are drastic differences between the 4s and iPad Pro. What type of constraints and thought processes should I be using?
The views being scaled may each have varying height and take up a different proportions of the screen. I know there are many methods to tackle this but what are the best ways.
Can the answer attach pictures so that I might clearly understand the process?
Thank u very much,
This is a pretty in depth question but to scale a view to the size of the parent and on down you need to use
Equal Width Constraints
Equal Height Constraints
Aspect Ratio Constraints
I use these very often. Here is a quick example
Suppose we want to divide the screen into 3 views. All with equal widths to the main controller view and we want the top to take up maybe 40% the 2nd view to take up 30% and the bottom to take up 20%. We can use equal heights constraints to the superview and edit and adjust the multiplier 0.2=20%.
To set a equal height constraint drag from the view to the parent view in storyboard and let go(you can also do this in the view hierarchy to the left Ex.Picture included).
You will see this menu.
Choose Equal heights and Repeated for equal widths. The width is fine in this example as I want it to be the width of the main view but we need to alter the multiplier on the height or each view will be the height of the main view.
Find the constraint in the size inspector and locate the constraint that says equal heights to superview(image below). Double click or click edit and bring up the menu in the image above. In this case I am setting the multiplier to 0.3 which is 30% of the parent. Now proceed to do this for all the views setting the percentage you desire.
OTHER Examples of Control Dragging to Parent Below:
I pinned all my views together.Meaning the top and bottom constraints are 0 between the views. I hope understanding these types of constraints and this is not an issue.
Example Picture of the pin menu. You can change these to 0 or whatever and quickly add Top,Bottom,Leading and Trailing for Any View
You can also use center horizontally and vertically and you can use a multiplier to keep relative position to parent view as well.
Now as to adding subviews to these views and having them scale proportionately. The process works the same way. See my final 3 big views and I am adding a smaller view to the top view I had just added that is taking up the majority of our screen. I want this new view to be like an avatar in a design.
I put it inside the top view and I am control dragging inside the small view without leaving it to get an option to add aspect ratio(notice I set the width and height equal in the size inspector before I did this step 90x90).
Let go you you will see this menu. This constraint means the view will always be square. It only needs to know its width or height and will solve for the other. Note:make sure the view is the ratio you want before control dragging in storyboard. See next image.
Now I add a top constraint of say 10 and a left of 15 to the view holding this avatar small view using the pin menu. Autolayout is still mad but all I have to do is add a width or a height since the aspect ratio constraint will solve the one we do not provide. I control drag from the small view to it's parent(anywhere outside the small view). I choose equal heights. I locate that in the size inspector to the right and change the multiplier from 1 to maybe 0.4. Now Autolayout is happy again as the small view knows how wide and tall it should be since we gave it a equal heights constraint and it knows that is supposed to stay square it solves its width.
Side Note:
If I don't want to use fixed top, bottom,trailing,leading you can adjust multipliers for align vertically and horizontally to work the same as equal widths and keep relative position.
I update the views and I can continue adding views. Here is what the result would be in Preview.

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.

How do I make two equally sized imageviews cover the whole screen

I implemented a viewcontroller similar to the one in this question:
iOS two views cover exactly half of parent view
I get the desired result. The problem is that when I add a UIImageView into those two containers, the result get resized and ruins the symmetry. How do I prevent Imageviews from changing the size of their containers? I need to use AspectFill for these images.
I don't think the containers' sizes are changed. You just need to set their clipsToBounds property to true to avoid overflow.
If you use Reveal you should see the containers' sizes are not changed even if pictures inside them are bigger than themselves.
I'm unsure I understand what you mean about adding UIImageView into those controllers causing the views to resize, so forgive me if I'm getting this all wrong...
It sounds as though you've created two views of equal height that, together, consume the entire vertical space of the screen. After having done that, you want to add one or more UIImageView's to each of the original two views. Unfortunately, when you add the UIImageView, the enclosing view is resized.
Assuming I have that correct...
Are you doing this with Interface Builder either in an XIB or Storyboard file? If so, you ought to be able to achieve this with the proper set of constraints.
In the following image, I've laid out what I describe above.
As you can see, I have a red view on the top half of the window and a green view on the bottom half. The red view contains a UIImageView that is 75% of the width and height of red view, with its origin at (20, 20) within the red view.
The scene is configured as shown below:
The constraints on "Upper View" are:
You can see from this that Upper View is flush with the left, right, and top of its superview, and that its space to Bottom View is 0. You'll have to trust me that Bottom View is set up the same way.
The height of the Upper and Lower views is "Proportional" as shown in this constraint:
To achieve this "Proportional" setting, you first make the height of Upper View equal to the height of the superview, and then edit the constraint, changing "Multiplier" from "1" to "0.5."
The height (and width) of the Image View is proportional to that of the Upper view, as shown here:
If you set it up this way, you ought to be able to accomplish what (I think) you are looking to accomplish.
If my original assumption of what you are trying to achieve is incorrect, please post images of what you've got and how it's not working.

iOS - Autolayout - Increase/Decrease view size proportionally on orientation change

I went through ray wenderlich's tutorial about autolayouts (link) and then began working on a demo project thinking I've figured it all out but I was wrong. As shown in the screenshot below I have a navigation bar, 2 views and 1 button. The layout on portrait mode looks fine without adding any constraints on views or the button, and understandably in landscape mode views are messed up. I tried adding following constraints on views and those don't seems to work.
View#2: Select view 2 > Editor > Align > Horizontal center in container (hides view completely)
Add Top space to superview. Again view goes away from both landscape and portrait preview.
If I can display view#2 correctly I am planning to add vertical space between view#2 & view#3 and then between view#3 and button#4.
My main concern is to resize the views so that it shows all views and buttons in iPhone 4s landscape mode. Any advise or suggestions are appreciated.
EDIT: Here's the end result that I am trying to get:
The reason your views go away when you add constraints is because a UIView has no intrinsic content size, so its size is {0,0}. The view appeared when you didn't add constraints because the system adds constraints for you, if you don't add them yourself; the system added ones are top, left, width, and height. So, you need to set the size of the views somehow. You can give them explicit size constraints, you can pin them to the edges of the superview, you can give them relative heights based on other views, etc.
Since you want the 2 views to get proportionally smaller in landscape, you should give them heights that are relative to the superview. You do this by selecting the view and the superview, and choosing "Equal Heights" from the pin menu. Edit that constraint to change the multiplier to something like 0.25 for the blue view and 0.2 for the orange one (this assumes that orange or blue view are the first item in the constraint -- if they are the second, then you should use the inverse values of 4 and 5). You should also do the same for the widths, since it seems you want them to get proportionally smaller too.

Resources