How to make view controller expand to fit layout on smaller screens? - swift

I am working on adding constraints to my view. I have everything perfectly laid out for the iPhone X and it looks great, but I would like my view to expand/scroll to accommodate smaller screens sizes. How can I transform my current view to scroll only if the screen size is not as tall as the iPhone X? ideally with as little reconfiguring as possible.

I follow this approach.
Put all your UI components inside a UIView say it ContainerView.
Now put ContainerView inside a UIScrollView.
Now select ContainerView, add constraints to make width and height equal as of scroll view.
Add a constraint of height on ContainerView. Set its relation as "Greater Than or Equal". Set constant value equal to the height which you want.
Set priority of equal height constraint to High.

Related

Trying to resize my UIView containers to fit smaller iPhone screen sizes

I am trying to have my layout fit on multiple iPhone screen sizes, but I am having trouble resizing my UIView's to resize themselves when on screen sizes smaller than the iPhone xr.
I have constrained the views, labels, and buttons properly and everything is aligned, but when viewing the app on smaller screen size, I don't fully understand how to resize my UIView containers to resize themselves to fit smaller screens.
On smaller screens, my button does not show up because the screen size is too small. The UIView's are the issue and the stack views inside them also need to resize or shrink the text to fit everything on the screen.
Image of my storyboard, and respective screen sizes
"I don't fully understand how to resize my UIView containers to resize themselves to fit smaller screens." -UIViews won't resize according to the screen, you will have to add constraint for them so that they pick their width and height. Alternatively you have to set constraint for fixed height and width.
In my opinion, for supporting smaller screen size, you must use scrollviews as parentview. So that user can scroll in the app. Also you can give relative width and height for views inside scrollview.
Scrollviews are required because you will always want some minimum width and height for buttons, labels etc. Otherwise on larger screen like Ipad they will be very large, while on smaller screens they will be very small.
You can use TPKeyboardAvoidingScrollView: How to use TPKeyboardAvoidingScrollView, or just google for that. It handles keyboard showing and hiding task, which is a headache otherwise.
Here is how I think you can solve your issue:
Set Some minimum height and width for your topmost view(scrollview preferably).
Now add other views inside it and use relative width and height. In relative width and height you give values in ratios. Here you can get an idea how to do that: -Giving width in % values in autolayout.
Also add additional constraint on your internal views for minimum height and width so they don't fall below certain size.
I would not suggest, but you may always use UIScreen.main.bounds.size.width and UIScreen.main.bounds.size.height to get the width and height of screen. And according set values for your constraints in your view/controller class. Here is a link for setting value of constraint in swift class:
set contraint value programatically

StackView not appearing correct on all screen sizes

I have a ViewController that contains a Stack View. In this Stack View there are 3 more Stack Views. The first contains 3 labels, the second contains more Stack Views with buttons and labels, and the third contains 2 buttons and a label. And at the bottom, there is a button. The layout is fine on screen sizes from 4,7" and bigger. But when the screen size is smaller, it cuts out the top Stack View. I have tried to set constraints on leading, trailing, top and bottom in different variations (with and without top and bottom constraints) But I can't make it work properly. I have attached screenshots of the different screen types.
EDIT* Depending on the selection from the previous ViewController, the middle Stack View(the one with the check boxes) can be isHidden When its hidden, the view is correct.
Generally you don't set height on UIStackView. They are dependent on it's content. This will cause your stackview to get out of the screen height. Have you used the view hierarchy? If you want to cover the small sized phones you could put your main stackView inside a scrollView to still be able to see all content.
It's a bit difficult to diagnose what's happening without seeing the constraints you apply to the views, however I'll recommend you some things that may help:
Set the space between views using size classes so you can set the right one depending on the screen size and avoid the overlap with the below views.
In case you are doing it, don't set a height constraint for any stackView.
To align the top StackView I would use not only a top constraint to the top bar (of type greater than or equal) but also a vertical center constraint with a right multiplier (with a high priority). In this way you ensure the top stack view is aligned vertically in proportion to the screen size and with the top constraint you ensure the stackView doesn't hide below the top bar.
Make sure you are hiding the right stackView.

StackedView inside ScrollView in Xcode 8 / Swift 3

I'm trying to arrange stacked views so I can set out the UI of my app to look neat and tidy.
The only problem I'm having at the minute is getting it set up so I can see it!
At the minute, it looks like this on Main.storyboard and it's annoying me:
Storyboard view
My constraints are currently set up as follows:
Constraints (edited)
Where am I going wrong?
Your UIScrollView needs to know its content's width. Since it can scroll both horizontally and vertically, it does not act like a UIView on interface builder.
One thing you can do is adding a hidden UIView in UIScrollView with 0 height and |-[WidthView]-| constraints (leading and trailing to its superview - UIScrollView). And then you should add WidthView.width = UIScrollView.superView.width constraint.
This makes sure that your UIScrollView's contentView's width is always equals yo UIScrollView's superView's width.
Your view hierarchy should look like this:

UIScrollView Causing "Misplaced Views" AutoLayout issues

I'm running into a strange AutoLayout related issue when I use a UIScrollView (the issue does not occur without it).
I have a UIScrollView that is constrained to the boundaries of a UIView (contained within a UIViewController), and within that, I am attempting to place a UILabel and UITextField side by side. I have constrained the UILabel to the left and upper boundaries, with it's width and height constrained (see screenshot below):
Right next to this UILabel is a UITextField, which is constrained to the left, top, and right, as well as having the height constrained. However, this results in a "Misplaced Views" warning, that states "Expected width = 163, Actual width = 413", shown in the screenshot below:
When I choose to "Reset to Suggested Constraints", the "Misplaced Views" issue disappears, but in it's place I am left with a width constraint of 413 points, which is something I'm hoping to avoid, as I would not like this UIViewController to be horizontally scrollable on smaller devices.
A scroll view has a size (the size it takes up on the screen) and a content size (the size of the entire scrollable area). In Auto Layout, the content size is automatically computed from the constraints of the items in the scroll view. This is a problem, because you are trying to make the scroll view have the same width as your screen, and then have the items constrained to that. When you do that, Auto Layout insists that you give your text field an explicit width so that it can calculate the width of your scrollable area.
To do what you want, do the following:
Add a "content view" to your scroll view. This view will be the only top level item in your scroll view. It will hold all of your content as subviews of it. Drag out a UIView and add it to your scroll view. Constrain its top, leading, bottom, and trailing edges to the scroll view. Constrain its width to the width of the scroll view. Give it a height constraint and set it however big you want your content area to be.
Add all of your labels and textfields to this content view. Now you can constrain them centered in your content view or constrained to the edges, and it will work as you want.

Dynamically set UIView size depending on height of grouped table view

Setup: I have a UIView with a scroll view nested within it. Within the scroll view I have a label, uiimage, and a tableview (grouped). The label, uiimage, and tableveiw are populated by a webservice. The last section in the grouped table view contains text that will never be the same and could be as long as 5 characters to 250+. Also, this view is pushed from a basic tableview (so it has a navigation bar. If that matters at all).
Expected: The uiview should extend in height depending on the height tableview extends to. Then I will be able to set the scrollview to accommodate the height I need to be able to scroll.
Problem: I'm not quite sure how to approach the issue. I really only know how to change the height to fixed values, which will not work properly in almost any scenario.
Am I using UIScrollView incorrectly? Do I need to resize the UIView at all?
You don't have to modify your UIView frame size, which has to be the size of your screen. The UIScrollView frame size must also be the same, it represents the part of its view actually displayed.
What must change is the UIScrollView contentSize, which defines height and width for data inside it ;)
You can calculate it using each inside element's height and by adding the correct margin.
Thus, you could have a UIScrollView content size of 320 * 600, which will let you automatically scroll down.
In fact, you have to display your content independently of the final frame size. If you have a content of 500*500, just display it inside your UIScrollView. Then tell it the size of it's content, and it will automatically set scrolling possibilities if needed.
Turns out I had to create a UIView programmatically and set it as the header of the UITableView. It works perfectly now. :)