0

Forgive me if this has already been answered elsewhere, but I couldn't find a thread about this:

I have a VC with the following hierarchy:

enter image description here

I want my ContentView elements to scroll within my ScrollView. I have a lot of definitions in the textView, and some are longer than others. Previously, only the longer definitions would scroll and the shorter ones wouldn't, but since I had an auto layout conflict, the links weren't able to be clicked at the end. I have the ScrollView constrained to the View and the ContentView constrained to the ScrollView. I also have the ContentView constrained equalWidth and equalHeight from the main View. In order for this solution to work, I had to set the height of the ContentView to something like 1000 pts so that longer definitions wouldn't be cut off. The only problem is that now all the definitions are scrolling (even ones that don't need to - that don't have enough text to go over the screen). And the user can scroll down and see enough white space to cover the screen, depending on how brief the definition is.

My question is: how can I arrange the constraints so that longer definitions scroll when needed and shorter ones do not - how can I set the constraint dynamically?

Thanks

jdlace
  • 341
  • 3
  • 8

1 Answers1

0

If I am understanding you correctly, it sounds like you are having issues with setting your scrollView's contents so that it will scroll when it is supposed, and not scroll when it shouldn't. Correct? Then (assuming your constraints are set properly) your scrollView's contentSize should be set by autoLayout based on the size of your Content View.

Try setting a property up to dynamically control the height of the Content View. In your ViewController, create your property:

@property (nonatomic, strong) IBOutlet NSLayoutConstraint *contentViewHeight;

Then, wherever you are setting the text of your labels, you should calculate the new heights, and update your constraint based on that. For reference, here is a question regarding how to determine the height of text in a label.

Once you have the height you can set it like this:

self.contentViewHieight.constant = // Updated Height
[self.view setNeedsUpdateConstraints];

I hope that helps. In my own experience, I have been limiting my use of UIScrollView since the way it has been updated to use AutoLayout. I mostly use UITableView & UICollectionView in its place.

Community
  • 1
  • 1
Aron C
  • 838
  • 1
  • 10
  • 17
  • Thanks, Aron. Yes, that's what I have in mind; dynamic resizing of contentSize. You said "assuming your constraints are set properly your scrollView's contentSize should be set by auto layout..." I'm pretty sure everything is wired up correctly (there are no auto layout issues) and it all resizes properly on different devices. Should it be resizing without the explicit property? Manually resizing every text view isn't really a solution for me. – jdlace Jun 15 '15 at 23:38
  • @jdlace I didn't mean that you should have to dynamically size each textView. You just need to get the height of all of the content in your contentView, and set the height constraint on it. However, now that I've thought about it some more, it should be simpler than that: You should not constrain the size of the contentView to the view. Instead, you should constrain the bottom and trailing space to the LAST element in your scrollView. If everything is constrained properly inside your Content View, then when the sizes of your textView grow so should the overall size of the containing view. – Aron C Jun 16 '15 at 01:59
  • Thanks Aron. Currently, these are my constraints: ScrollView (top, bottom, leading, trailing) to MainView. ContentView (top, bottom, leading, trailing) to ScrollView AND equalWidth to MainView. Then, for the textView within the ContentView, I have (top, bottom, leading, trailing). Then I'm setting the height constraint property using self.textViewHeightConstraint.constant = [self.sourceInformation sizeThatFits:CGSizeMake(self.sourceInformation.frame.size.width, CGFLOAT_MAX)].height; So far, I'm having mixed results; definitely resizing dynamically based on text, but still not 100%. Thoughts? – jdlace Jun 16 '15 at 17:48
  • @jdlace Wrong copy and paste. Editing now. – Aron C Jun 16 '15 at 18:31
  • @jdlace (Took too long to edit.) It sounds like your constraints are configured incorrectly. [Here is a link to a blog explaining the pure-autolayout approach to ScrollViews](https://codehappily.wordpress.com/2013/11/14/ios-how-to-use-uiscrollview-with-autolayout-pure-autolayout-approach-multiple-smaller-views/) Hopefully that helps. What part isn't working 100%? – Aron C Jun 16 '15 at 18:42