JWKot
JWKot

Reputation: 360

Resizing a UITextView in UIScrollView using Auto Layout issue

I really hate to ask here because I usually try to figure things out on my own. But on this one I've stuck for days and can't find a solution anywhere online.

I have a ScrollView containing multiple subviews. I've got an image view and two labels at the top with fixed heights. Then there is a UITextView and another ImageView (see pictures).

I add the text to the text view programmatically so it should have a dynamic height and the ImageView should move to the bottom so you can scroll. I don't want the TextView to be scrollable in itself but I want all the subviews to move as well.

I know I should be able to solve this issue using constraints. But I feel like I've tried everything and nothing worked yet. It worked when I disabled auto layout and moved the views manually. I'm wondering if there is a better way though.

enter image description here

As you can see I pinned the TextView to the ImageView above with a 1,000 priority and to the ImageView below with a 1,000 priority. The height constraint can not be deleted so I set it to the lowest possible priority. The ImageView on the bottom is pinned to the bottom of the superview with an absolute height. Its height constraint also has low priority. (I can post an image of the ImageView's constraints, if it helps) I also tried adapting the frame programmatically but this is not working well in combination with auto layout. (If it helps I can of course post the code)

What am I doing wrong? Shall I just disable auto layout and do it manually? This seems unclean to me. Is it even possible to do?

I really appreciate your help :) Greets, Jan

Upvotes: 2

Views: 4218

Answers (3)

JWKot
JWKot

Reputation: 360

If somebody is struggling with a similar problem: This is what I ended up doing:

  • Remove all subviews from the ScrollView in IB
  • Programmatically add a single UIView to the ScrollView.
  • Add all the views to the UIView as subviews (move them using setFrame)
  • Set the Frame of the UIView appropriately to the subviews
  • Set the ScrollView's contentSize to the size of the UIView.

A little more work but it finally works. This follows Apple's mixed approach guidelines that can be seen here (look for UIScrollView): http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/index.html

Upvotes: 1

Maarten
Maarten

Reputation: 1873

Make sure the Scrolling Enabled attribute on the UITextView is unchecked in Interface Builder. I believe that the Auto Layout system takes that into account when it calculates the intrinsic content size.

Upvotes: 3

Mundi
Mundi

Reputation: 80265

The problem is the height setting. You somehow have to try to delete it. If you have added other constraints that are "sufficient", it should become deletable.

At the moment you have one user constraint for the height that is "Greater or equal" and an "Equals" constraint as well. Clearly, those are not working well together.

Maybe there is a conceptual error as well. The lower image view should not be fixed in position, so the distance to the lower image view will not be a "sufficient" constraint to let you delete the fixed height.

I think it should work if

  • the lower image view has a fixed height and
  • a fixed distance to the text view above, and
  • the text view has a minimum height as well as
  • a fixed distance to the image view above
  • (which should be fixed in relation to the superview).

Upvotes: 0

Related Questions