0

I'm trying to allow the height of the subview (the white box inside the view. The view controller swift file is a separate XIB file) depending on the amount of content in it. How do I do this?

Imgur

This is what I have so far for it:

@IBOutlet weak var myScrollView: UIScrollView!
@IBOutlet weak var myContentView: UIView!

override func viewDidLayoutSubviews()
{
    let scrollViewBounds = myScrollView.bounds
    let containerViewBounds = //I am not sure how to do the rest


}
Kody R.
  • 2,292
  • 4
  • 17
  • 41

1 Answers1

2

The best way to achieve this is to use autolayout. You have great tutorail here:

http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1

If you don't want to use it, set reference for your view inside your view controller for that view, and use various functions to calculate height. There are several ways for labels ( Adjust UILabel height to text ), text field( How to set UITextField height? ), etc.

Community
  • 1
  • 1
Miknash
  • 7,602
  • 3
  • 29
  • 44
  • Thanks for the article! unfortunately it's in objective c but i'm coding in swift. The subview above will hold either a lot of information or a little depending on the user. Does auto layout have an affect with that? – Kody R. Jul 12 '15 at 00:28
  • yeah, you can use it for everything UI related. If you need some of the functions that are written in Objective-c, just let me know and I will rewrite it in swift. Its not that big difference :) – Miknash Jul 12 '15 at 00:29
  • Dude that's great and such a big help! And yes the two are pretty close in writing (though I think Swift looks much more of an attractive language :)) I'm going to take a look at it. The subview I have will hold different types of collection views (depending on which of the segmented control choices is selected). The size of the cells themselves won't change, but the number of them will. – Kody R. Jul 12 '15 at 00:38
  • Yeah, swift is Javascript like and easier to learn. Glad to help, happy coding – Miknash Jul 12 '15 at 00:42
  • So I read a little about it. I'm familiar with how to use auto layout, just not with subviews that will change size. So if I understand correctly, my XIB file has to communicate with the subview to let the subview know how much vertical space it needs to fit on the page (depending on the amount of content). Then the subview needs to communicate with the scroll view to let the scroll view know how big it should be? – Kody R. Jul 12 '15 at 01:16
  • yeah, thats correct. you need to keep reference(s) to your xib files in order to give them data, and to get their sizes back. Also, there are few functions you would like to take a look: setNeedLayout(), setNeedsUpdateConstraints(), viewDidLayoutSubviews(). There are some more, but these are on top of my mind right now. last one will let you know when all the autolayout did the work, and in that function you should update scroll view. – Miknash Jul 12 '15 at 01:19
  • Yeah, in the code above I (kinda started) using viewDidLayoutSubviews. I'm new to coding (I've only been coding for a few months). What do you mean by keep references of the XIB files? I'll take a look at those functions now! – Kody R. Jul 12 '15 at 01:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83054/discussion-between-nickcatib-and-kody-r). – Miknash Jul 12 '15 at 01:26