How to achieve this simple screen with UIScrollView and AutoLayout - ios

First of all, I understand the basic of using UIScrollView with AutoLayout by reading: https://developer.apple.com/LIBRARY/ios/technotes/tn2154/_index.html
What I want to achieve is:
UIScrollView takes up the full screen height
UIButtons are positioned like the above wireframe
I understand the contentSize of UIScrollView is determined by the position of the button at the bottom and its bottom space constraint to the UIScrollView, problem is I couldn't find a way to properly position the button at the bottom to make it work for 3.5" and 4" respectively

Put your UIButtons & UIScrollView in a parent UIView. Do not include buttons in scrollview, but instead put them in parent view. Arrage them as per your layout.
Now select the button & go to Editor -> Pin & click on Height. For top buttons pin Top Space to Superview & for bottom buttons pin Bottom Space to Superview.
For your scrollview & parent view pin Top & Bottom Space but DO NOT pin height, this will strech it to 100% height, while keeping button of same height & at their respective place.

Related

why does my view embedded in UIScrollView scroll only a little?

In my UIViewController I put a UIScrollView and attached constraints:
Then I added a view to this scroll view with following constraints:
and positioned it like this:
so as you can see it is quite long. When I run the app I can scroll it just for couple pixels instead of completely to the top of the screen. I want to make it possible to scroll it to the top so that it can cover everything what's under it. So what am I doing wrong here?
========= EDIT
for clarification - I want to achieve an effect that when user opens this screen, 10% of the screen is covered by the view. User can slide up this view and then it covers 90% of the view. And he can slide it down back to the 10%. Can you help me with adjusting constraints so that it looks good on every screen size?
Take a UIView inside the UIScrollView.
Set the constraint for scroll view as leading, trailing, top, bottom.
Select UIViewController in the story board and click size inspector then choose freeform then set the view height as bigger.
Then set constraint of UIView is leading , trailing , top , bottom as 0, so that the UIView will be same as ScrollView region. Now set height constraint something bigger so that you can design image view, label 1 and label 2. Then set constraint like the image view : top to UIView, leading , trailing and height.
For label 1: leading, trailing , vertical to UIImageView, height.
For label 2: leading, trailing , vertical to label 1, height and bottom to UIView (Important as based on this content view will be set).
After setting constraint You can select UIViewControl from story board and set the SizeInspector as fixed as it was before.
It will show the screen as you need (Image view, below label 1 and on scrolling label 2)
I have tested it in my demo project.
Hope it helps

How to Re-Size Label to fit any screen size

Ok, here's what I'm trying to do:
I would like to create a scroll view to scroll vertically, in case my text is longer than the screen size (think iPhone 4 vs. iPhone 6).
I've added a scroll view and placed a view within that. I embedded by label for the text within the view.
However, the scroll view currently will scroll only horizontally.
Is there a way to constrain the label width to be the width of the screen? How do I do this?
You can constrain the width to contentview to be equal to scrollview width. And pin the four sides of label to the content view.
Also you will need to pin all sides of content view to scrollview, if you haven't already did that.
If you push the constraint button on the bottom of your storyboard pin it to both the left and right side. Uncheck constraint to margins and pin it both 0. :)

Can't build UIScrollView with Autolayout properly

Can't build content inside scroll view with autolayout. Yes, I've investigated a lot, read apple technical note and some articles, I know, that content view should be constrained with scroll view's superview but can't still achieve the desired result. I expect this result:
fixed or scrollable (for small screens) portrait mode
always scrollable landscape mode, not to loose or overlap my fixed content
my button (red view) should always be at the bottom
my photo (yellow view) should always be fixed size and not to loose content
(like in my wrong landscape mode)
Making this work is actually pretty easy. All you have to do are the following steps:
Add the UIScrollView as subview of your UIViewController`
Add the UIView as a subview of the UIScrollView
Add the UIButton as a subview of the UIViewController. Do not add it as a subview of the UIScrollView if you want it to stick to the bottom.
Then add the following constraints:
Pin the UIScrollView to the top, left and right of you UIViewControllers view
Pin the UIButtonto the left, right and bottom of your UIViewControllers view
Pin the UIButton top to the UIScrollView bottom
Pin the UIImageView top, horizontalCenter and bottom to the UIScrollView
Set the UIImageView height and width constraints
Set the UIButton height constraint
And that's it. Do not add anything to the UIScrollView contentView and do not set the contentSize programmatically! The contentSize ist set automatically by Auto Layout.
Here are screenshots of the constraints and the result:

UIScrollView has an unexplainable position in UITabBarController with autolayout

Using Xcode to create a new project, a Tabbed Application.
On the first view controller I add a UIScrollView, bind it to the view for all borders. Then I add a UIView "Content View" with equal width as the superview of the UIScrollView. I add a label on the top and a label on the bottom of the content view, and make sure that the size of the content view amounts to a higher height than the height of the scroll view, so that we get a scroll.
Still I get a horizontal scroll bar in the view. Why? The width of the content view is bound to the width of the superview of the scroll view, which should thus be as wide as the device, and give no scroll. What am I missing?
The project is downloadable from here if you want to play with it.
Your problem is that you have two constraints for the ScrollView which are set relative to the margin.
If you make the leading and trailing constraints relative to the superview with no margin then your code works as you require with no horizontal scroll bar.

Using autolayout constraints to define height minus a constant with storyboard

Is it possible to have a View with autolayout , such that the subview has equal height with parent minus a constant?
The Hierarchy is UIView > UIScrollVIew > UITableView. The view is also embedded in a UINavigationController
Yes. You simply pin the subview to the top and bottom of its super view with the constant set to the offset you wish.
As the outside view changes, the inner view will change to match and maintain the offset. You will need to set left and right constraints as well using the same mechanism.
Edit after discussion:
The UIScrollView needs to know the size of your content. You can not simply use auto layout without a few adjustments.
Assumption is your view layout is:
UIViewController -> UIScrollView -> UIView -> UITableView
For your UIView, pin the top, bottom, left and right to the UIScrollView.
To see things properly in storyboard you also seem to have to center the UIView horizontally and vertically. Once you add this you should see the content fill the scroll view bar any edge offsets you set in your pinning.
Pin your UITable to the UIView top, bottom, left and right.
You are going to have to adjust the width in your code somehow to the size you want to scroll horizontally.
In Interface Builder:
Clear constraints on your subview
Select your subview
Choose Pin in right bottom corner of IB view
Select bottom pin and enter your constant (I entered 30), select top pin and enter 0.
Select other constraints if needed.
Accept with button Add x constraints

Resources