Center multiple views with varying height using autolayout - ios

This answer: https://stackoverflow.com/a/25898949/1364053 is suitable for my problem to some extent. What I need is there are three views in my container view.
The middle one is a scroll view. If I pin height and width of all three views and use the ratio method as described in the above link, all works fine.
But I need the middle scrollview to grow to some extent, let's say the current height of the scroll view is 30, If the device height is increased it should grow to a max of 40 but not more than that (<= constraint).
Ok..So I tried giving two constraints to the middle view. The <= constraint has higher priorty (750) than the = constraint (250).. but when the screen size is increased.. the height stays the same.

Sample.xib
The uploaded xib might help you.

Related

iOS Auto Layout - Impossible case?

I need to create a launch screen Storyboard with 3 views. The middle view must have a 4:3 ratio, the top view must occupying 30% of the remaining space and the lower view must occupy the rest of the space.
For example, when the view is 375 x 667...
Middle view: 375 x 4/3 = 500
Top view (667 - 500) * 30% = 50
Lower view: 667 - 500 - 50 = 117
I tried everything I can think of (including using a UIStackView) but can't find a way to make it work.
Is it possible? What would be the right way?
Many thanks!
You can't easily do that with autolayout, as you rightly say, because the thing your first view should occupy 30% of is not a contiguous piece of space. You can do it by adding two invisible views, to measure the "remaining space" with the height of a single view that you can take 30% of, but it's not worth the trouble. Just implement your requirements in code directly in layoutSubviews or similar.
It's possible, You can archive this by using equal height constraint with proper multiplier. Following are the steps in your case:
Add all 3 views with required size as per screen size.
Set equal view hight constraint for first view to super view as bellow.
(Multiplier is set by considering you are working on view with size 375 x 667. Change it as required.)
Set top, left & right constraint as required to zero.
Set equal view hight constraint for second view to super view as bellow.
Add other missing constraints to second and bottom view.
You are done.
To get the height of the remaining space, I added a hidden view at the top with a 4:3 ratio contraint and another hidden view below that occupies the rest of the space. I coud then use height constrains relative to that second hidden view to layout my views.

Change UImageView size using Autolayout

I have a imageview at the start and below there are series of buttons placed vertically one below the other. So, while designing the screen, I placed all the views in the view controller and added to vertical stack. Centred stack view horizontally and vertically. For all the buttons, I have set equal width. And in stack view, I have given equal spacing. In all iPhones, it is working correctly with ui elements arranged correctly with equal spaces except iPhone4. In iPhone 4 it is all touching each other. Please find the attachment. What constraint am I missing here?
The problem is the content cannot be fitted in given height.
Let me explain.
Assume small device has height of 480. UIImageView height(intrinsic ) is say 200. You have 4 UIButton of height say 80. UILabel of height say 100. So that sums up to 620.
Views of height 620 cannot fit(all visible without clipping) on a view of height 480.
You need.
Scrollview(To show more content than the available width and/or height)
Reduce the size of you content to fit in 480. To do this you can pin the height of any/some/all to make this height sum less than or equal to 480.
It's because the screen size for iPhone 4 is 480. Your UI seems to be higher than 480. So only the constraints adjusted according to screen height.
Just add height constraint to all button and remove height constraint for image view if added.
Add Height and margin constraints for Buttons and only margin constraints to image view. It will resize according to screen size.
Hope it helps.

autolayout view gets overlapped on smaller devices

Basically I have one view with some height x
and 4 other views with same height y all these 5 views are vertically one after another with 10 px space
all these views have again some child views which depends on them.
For this scenario stackview is ideal but it support starts from iOS 9 unfortunately I have to support from iOS 6
so basically I fixed all the views with leading,trailing,top,bottom and height constraint...
it works good in iPhone 7s ... but in iphone 4s the view gets overlapped on each other
in iphone se the views are very much congested..
I was always thinking the height will change according to the screen size for iPHone 4s but that didn't happen
How can I work this out ?
There are two ways to solve the problem you are facing.
First:
Instead of giving a fixed height to any of your UIView you can use a proportional height constraint. Select your UIView and control + drag from your UIView to the main superview. Select equal height constraint. Now, double click the height constraint and set the multiplier to 0.15 or any value you seem nice. This will ensure the view thus created is always 0.15 times the entire view's height. Now you can create your other views either proportional to this view following the same steps or to the superview.
Second:
(I prefer this approach as for items as sometimes you need to create forms the above approach might still push elements off the screen).
This will use a UIScrollView. To your main view add a UIScrollView and add a leading, trailing, top and bottom constraint to your superview. Add a UIView to this UIScrollView and give it a leading, trailing, top and bottom as well. Additionally give it a equal height and width constraints to the superview of the UIScrollView with a low priority of like say 250. Now add all your elements inside this UIView however you seem fit. With fixed height, proportional height whatever and happily run it. But ensure you add a bottom of >= a minimum value for your bottom most view.
The screen will automatically become scrollable if the content will go off screen otherwise it won't be scrollable at all.
fixed all the views with leading,trailing,top,bottom and equal height constraint instead of only height(not give fixed height to any view)

Height and Width of buttons not the same (ios)

I have a grid made of iOS buttons (think Sudoku). I assigned each button to have aspect ratio 1:1. To further ensure their equality, I have made each button have width = height constraint set.
However, when I look at the buttons, some of them have 44x45, while others have 44x44 (as expected).
Is there a constraint that I'm missing or is there a workaround that can guarantee my buttons are perfect squares.
This is probably happening because you have other constraints affecting the views too. For eg. You might have put these in a container view and specified container views width or made container view's width equal to the width of screen.
Now when these square view fill the container view, then there is some extra space left say for eg like 7 points. What autolayout does is that it adds these 7 points making some of your squares wider by 1 point.
This can be fixed by providing the correct constraints for the container view.

How to correctly use constraints when both UITableView and UIImageView are presented on the same view controller

Suppose that I have the following view controller and this is how I want to see it on all iPhone:
If I run it on iPhone 6 it has the following look:
Here you can notice that UITableView not fit the whole screen and UIImageView doesn't placed at the bottom of the screen.
How can I achieve the required behavior via constraints in XCode 6? I thought that I need the following constraints:
Leading space and top space to container margin for UITableView
Bottom space and trailing space to container margin for UIImageView
Vertical Spacing between UITableView and UIImageView
But it doesn't work as expected even after auto-resolve constraints issues:
Thanks in advance.
Ok, a few things here:
Each view needs enough constraints to define it's x and y position, and it's width and height unambiguously. To start with, go back to Interface builder and delete all of your constraints and lay out the view as you would like it to look. You want to have control over every constraint added, don't let IB automatically resolve the issues, as in all likely hood it won't do what you want.
Do you have an image that is the size you want it to be on screen, once you've factored in #2x, #3x etc? If so, then your job will be easier, as the width and height of the image view can be defined by the width and height of the image (ie the image view's intrinsic content size).
In order to use Autolayout effectively, you need to think about your view holistically, and think about how you want your views to behave when the screen size changes, be clear in your head about the behaviour.
To achieve the layout you want, I would do the following:
Constrain the tableview's leading, top and trailing edges to the superview, with a constant value of 0. This means it can get wider and thinner with the device, it will stretch horizontally, but always stick to the top. This has defined the tableview's x and y position, as well as it's width (height still to go, but keep reading...)
Constrain the image view to match the horizontal centre of it's superview (x position defined) and constrain it's bottom edge to the superviews bottom edge (y position defined). If've you've got the right sized asset, then that will take care of the width and height too. If not, you could go ahead give it explicit width and height constraints.
Now we can constrain the tableview's bottom edge to the top of the image view, with a constant of 0 (ie touching). Note we haven't give the table view an explicit height constraint, so as the device screen grows vertically, the table view will stretch vertically.
Autolayout is hard at first. I'd recommended lots of reading to get over the initial hump, really get to know what a constraint is doing, it's limitations, and the way in which the system parses constraints to translate them into frames. This book is really good, and really helped me learn:
http://www.amazon.co.uk/Auto-Layout-Demystified-Mobile-Programming/dp/0321967194
Best of luck
First make sure you have selected the correct size class. The 'Compact Width | Regular Height' size class must be selected in the Interface Builder. Now add the Trailing space,Leading Space, Top space and Bottom space constraints to the table view. For the image view set the view mode to Aspect fit and add the constraints : Align Center Y ,Top space,Bottom space, Leading space, Trailing space and Aspect Ratio .

Resources