M Swapnil
M Swapnil

Reputation: 2381

dynamic height of of scrollview subviews in autolayout ios

I am creating a UIScrollView from xib, in which 3 view are there 2 UIViews and in middle an UIImageView. when I am setting constraints Xcode asked to set Y position constrains. But the problem is Y position constraint is blocking Scrollview to scroll down and automatically adjusting the views which looks ugly in landscape mode.

enter image description here

when I am delete that constraint it ask to fix height of subview. I searched a lot but I am new in autolayout so not understanding many of solutions. any help would be great.

Upvotes: 0

Views: 1864

Answers (2)

Rikh
Rikh

Reputation: 4222

UIScrollViewcan be tricky when adding constraints. You should always add a subView that will behave as the content view for your UIScrollView and all your subsequent views will go inside this content view.

UIView1
|---UIScrollView
    |---UIContentView
        |---UIView2
        |---UIImageView

Set your UIScrollViewconstraints as you would normally but set your content view to have leading, trailing, top and bottom to the UIScrollView but also add two more constraints which will be equal width and equal height to the viewController.view but will have a low priority (So that whichever direction your content will increase in, that constraint will break and automatically increase the content size of the scroll view by taking in the inferred height of the content view). Now go on and add constraints on all your subview as you normally would. Which i'm assuming would be:

  1. Your topmost view will have top and leading and trailing to its superView and also a fixed height.
  2. Your bottom view will have leading, trailing and bottom to its superView and also a fixed height.
  3. Your UIImageViewwill have a leading, trailing and top to top most view and bottom to the bottom view.

Edit: Here is the screenshot just in case (To show the view hierarchy with the content view's constraints in the inspector)

enter image description here

Upvotes: 0

jalone
jalone

Reputation: 2073

You have to set all the height constraints in the content view. But you also want the height of the Content to be proportional to the screen size.

To do this assign the height constraint of the imageview [equal|proportional|a-computation-of] to the view containing the UISCrollView.

It seems weird to skip levels of herarchy when assigning constraints between two views whose are not direct ancestor/sibling of each other but within a scrollview (at least) it is perfectly acceptable.

You are basically telling the scrollview that it's content has a known size and at same time setting this content to adapt dinamically to the screen size (if the constraints of the root uiview are set correctly)

UIView1
|---UIScrollView
    |---UIView2
    |---UIImageView [heightConstr.constant=UIView1.height-UIView2.height-UIView3.height-margins]
    |---UIView3

This is the basic idea, to be done programmatically, then you can explore other solutions.

Unfortunately the constraint system in ios pretty much sucks when it's up to more complex equations involving more views for a single constraint.

Upvotes: 2

Related Questions