UIScrollView and Autolayout prevent contentview compression - ios

In IB I have a view controller that contain a scrollview.
The red view is inside the scrollview
The minimum height of the red view is 504px, so on iphone 4 it should scroll and on iphone >=5 it should extend and layout the buttons to fill the blank.
I set the constraints of the red view to 0 from top, leading, trailing and bottom of the scrollview and also centered horizontally and vertically.
My redview has a minimum height of 504
The blue and green views have equal height
Everything is fine for iphone >=5 but for iphone 4 the red view is compressed to be the size of the scroll view and the buttons are touching each others.
After multiple constraints adjustments I'm wondering if it's even possible to do what I want 100% in IB with autolayout

You say (emphasis mine:)
"I set the constraints of the red view to 0 from top, leading, trailing and bottom of the scrollview and also centered horizontally and vertically. My redview has a minimum height of 504"
First, I can see some redundancy (the bold part).Your redView does not need the vertical constraint since you have already pinned it on the edges of the scrollview and has a defined minimum height.
But this could or not be the cause of the buttons coming closer together, depending on the rest of the constraints.
Have you set vertical space constraints for the buttons?

Related

XCode stackview adding elements

Im very new to xcode so bear with me. I added a vertical Stackview and want to add a bunch of textviews and buttons starting from the top. I managed to use constrains to stretch the Stackview to the screen on all sides.
Now when I add 3 textviews this happens, and its all wrong: first the textviews stick to the bottom for some reason, second one stretches out the remaining space which is obviously wrong. What can I do to make it correct, i.e. all textviews should be at top right under each other with correct width and height (width fills the screen, height is whatever the default height is)
A UIStackView will arrange its subviews.
If you set a Height - either explicitly or by constraints on the Top and Bottom, the stack view will arrange the subviews to fit into that height - when set to Fill or Fill Equally, it will "spread out and resize" the subviews.
If you don't constrain its Height, it will still arrange its subviews, but it will expand its own height based on the heights of the subviews.
So, for your case of 3 text fields, constraining all 4 sides results in the subviews being arranged to fit the height of the stack view.
If you constrain the stack view only to Top, Left and Right, you get this (main view is blue, stack view has spacing set to 8):

AutoLayout UITableViewCell

I am working with autoLayout for UITableViewCell
So here is my xib and constraints
Here I set constraints such that tableView has dynamic height
Whenever I run on iphone5, it looks this way correctly which I want
But when I run the same thing on ipad it shows this way
So I am not understanding how to make the ipad version look same as iphone version, Not understanding which constraints I am missing.
When working with autolayout constraints, formulate what you want into sentences.
Example:
I want the yellow view to be pinned to the right.
I want the yellow view to be pinned to the top and bottom.
I want the yellow view to have width of 50.
I want my label to be pinned at the top and bottom.
I want my label to be pinned to the left.
I want my label to be pinned to my yellow view, with 10 pixels between them. (Thus growing in width along with the superview width).
And there you have all your constraints. Now you just have to add them one by one. Top, Bottom, Right to superview and Width constraint with a constant of 50 for the yellow view. Top, Bottom, Left to superview and Right to Yellow view with constant of 10 constraints.
You've pinned your yellow view to the left of the superview, so on bigger screens, it will grow to fulfill that constraint.
It looks like you pinned the left edge of the yellow view to the left edge of the table view cell with a 300pt offset. That means on the iPad, the yellow view is still 300pts offset from the left edge of the screen, and grows to fill the rest of the width available.
What you probably want to do instead is pin the right edge of the yellow view to the right edge of the table view cell with a 0pt offset, then also pin the yellow view's width to its desired size.

UIScrollView in Storyboard not working with iOS 8 Size Classes and Autolayout

So I'm trying to create a UIScrollView only in storyboard that allows me to add scrolling labels for more than the height of the VC. Here's what I did:
Created UIScrollView that took up the size of the any width any height VC
Made constraints 0 for spacing to nearest neighbor on top, bottom, left, and right
Created a view that is a subView of the UIScrollView with the same width as the any width any height VC but height of 1500 (because I only want it to scroll vertically).
Set constraints to nearest neighbor as 0 for ONLY left, top, and right and set the height constraint as 1500.
I put a label at the top of the subView and at the bottom
When I run the app on an iPhone 6, does not scroll vertically as I want it to. Any ideas why this is not working? Thanks in advance.
To obtain the scroll you have to pin the sub view (the "content view") to the top, left, bottom and right of the scrollview.
In addition you have to give it (to the "content view") an explicit (=not related to the scroll view) height and width because these are used by the scrollview to calculate its content size.
In your case set the width equal to the VC main view width and the height to 1500.
When width or height are bigger than the scrollview size, it will scroll.
Scroll view constraints (pinned to main view)
Content view (pinned to scrollview + height 1500, width=mainview width) + label constraints (as an example: 20 20 from content view top left)
For an easier visualisation, I created a video on how to do that.
Video on how to create a vertical-only scrollview in iOS
Have you set the contentsize bigger than the screen itself? In your case, just bigger in height.
As Apple Documentation says:
"You must set the contentSize property to the size of the scrollable content. This specifies the size of the scrollable area."
The only solution is add constraints to the right and bottom of the bottom subview of scroll view's child view.
Also check if all the views from top to bottom have got proper constraints along with height.
For the scroll view ' s wrapper view add equal width and equal height constraint to its superview.

Correctly Size UILabel inside UIScrollView using Interface Builder and Autolayout?

I've created a simple view setup in interface builder. Here's how it looks:
The view hierarchy is simply:
view
- scrollView
-- label
The scroll view is shown with grey background anchored to its super view top, leading, trailing, and bottom with constraints of 0.
The label is shown with yellow background and has constraints as shown. Additionally, the label has content hugging priority of 1000 for both horizontal and vertical, and it has content compression resistance priority of 1000 for both horizontal and vertical.
In portrait orientation, the label is sized correctly:
In landscape orientation, however, the label is not sized correctly horizontally (I intended for the label to fill the width of the screen, less constraint insets as shown):
How can I get this label to size correctly horizontally in landscape orientation?
There is one solution for you.
1. Add your UIScrollView to container (UIView) with zero constraints:
2. Add constraints for Label: top, bottom, leading, trailing spaces = 20.
3. Add constraint: label.width = container.width - 40
For this, select label in the view structure tree, tap ctrl and pull
to container. And select Equal Widths.
Then select the created constraint and go to its Utilities and set
the constant value to 40.
You should get the following components:
Run the app, go to landscape and it works!
Hope it is clear. Best Regards.
It's hard to tell which constraint(s) to add/remove in order to get what you want because iOS reserve the right to adjust your constraints whenever it becomes impossible for it to satisfy all your constraints.
I make a blank project with the same view you have (UILabel as subview of UIScrollView) and make some constraints to get the UILabel resized properly on landscape.
A must check though:
Make sure you set the vertical/horizontal spacing constraints from the pin option, as shown below and try to remove unneeded constraints manually.

iOS Auto Layout >> View is not Changing its Size

I have a design for a screen that should look like this (other things will be added later, but I cannot seem to resolve the basis...):
I have added Constraints to determine the following:
Both Labels are Constraint in spacing to the screen edges.
Middle View is Horizontally and Vertically Constraint to the Middle of the Background View Center.
I have added 4 Constraints to express Minimum and Maximum Vertical Spacing between the Middle View and the Labels (Current spacing as Maximum and Standard spacing as Minimum).
I have also added 2 Constraints to the Middle View to define Spacings from the Screen right and left edges.
I thought that it should be enough, but in reality, when switching between Retina 3.5 and 4 the Bottom Label disappears and the Middle View is cut in the middle:
I have tried lowering the Middle View Content Hugging and Content Compression Priorities, and still no good.
Here are the Warnings I get:
Any idea how to resolve this?
Or alternatively, how to approach it differently (preferably, still using Auto Layout)?
Add Equal Width & Equal Height constraints as well & It will work
Add TopSpaceToContainer constraint for Top Label. Then add width and height constraints for your yellow view at the middle. Remove the multiple vertical spacing constraints given to the Top Label and Bottom Label.

Resources