0

I have a scroll view. On the scroll view I have added many necessary UILabels and UIButtons etcs.

Now I want to add MORE labels and buttons but I have no room on the interface of the view to add them, otherwise they will overlap the other elements. How do I add more UI elements so that they can be interacted with when the user scrolls? I was thinking if I hold the UIelement over the bottom of the scroll view, the scroll would "scroll down" and present me with more space.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Cescy
  • 1,921
  • 3
  • 18
  • 21
  • 1
    You could design a UIView with all this content on it in a `.xib` file and make the view whatever size you wanted, then put it into the scroll view in `viewDidLoad`. – nhgrif Jan 16 '14 at 01:17
  • 1
    Reading this [section of my book](http://www.apeth.com/iOSBook/ch20.html#_creating_a_scroll_view) might help, esp. the second half of the section (designing the scroll view's contents in the nib editor). – matt Jan 16 '14 at 02:47
  • Here are step by step instructions: http://stackoverflow.com/questions/20502860/scroll-view-not-functioning-ios-7/20503513#20503513 – Yas Tabasam Jan 16 '14 at 03:05

3 Answers3

1

In the Interface Builder, as far as I know, you cannot scroll the Scrollview. If you just want to add more labels and buttons into your scrollview in the Interface Builder, you can set the size of the View Controller which holds your scrollview to be Freeform in Attribute Inspector. Then you can set the size of the View to any value you want in Size Inspector. After that, you can change your scrollview's size to add more labels and buttons.

SamirChen
  • 1,209
  • 1
  • 12
  • 22
  • is this a common practise? doesnt seem like it – Cescy Jan 16 '14 at 02:07
  • @CescSergey If you're "new to iOS design" how would you know what's common practice? It is in fact quite standard. – matt Jan 16 '14 at 02:45
  • I don't know, instinct?, I should be able to set up the scroll view from the scroll view, not the view controller which holds it? – Cescy Jan 16 '14 at 04:37
1

The key thing here is to remember that a view controller's main view in a nib (.xib file or storyboard) is always resized anyway when it is put into the interface. The size you see it at in the nib editor (Interface Builder) is just a "serving suggestion". Thus, SamirChen's answer is quite right: set the view to Freeform so you can resize, and just make it big enough to hold the scroll view which itself is big enough so that you can put in all the desired contents.

I would add just two points that you will find helpful:

  • Use autolayout outside the scroll view. This will cause the scroll view to become the right size when the view is resized to fit the interface, and will cause everything else to be repositioned correctly.

  • Use autolayout inside the scroll view too! This is the really cool part. If you set up sufficient constraints between all the contents of the scroll view and the scroll view itself (their superview), the contentSize will be calculated for you automatically using constraints from the inside out! Thus, you don't have to know the content size or set it in code!

It is easier to see than to describe, so download the example code from my book and look at the examples currently entitled "bk2ch07p367scrollViewInNibAutolayout" and "bk2ch07p367scrollViewInNibAutolayout2".

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

"You could design a UIView with all this content on it in a .xib file and make the view whatever size you wanted, then put it into the scroll view in viewDidLoad" referenced from @nhgrif.

Don't forget to change the scroll view's contentSize property to meet the UIView's bounds.

KudoCC
  • 6,912
  • 1
  • 24
  • 53
  • @CescSergey As far as I know, there is no place to set the `contentSize` of `UIScrollView` in IB. – KudoCC Jan 16 '14 at 02:11
  • so everytime I add a `UIelement` as a `subview` do i have to call `= self.view.bounds` or can I do it once at the end of `viewdidload`? – Cescy Jan 16 '14 at 02:13
  • @CescSergey The UIView you add your ui element on is not the UIViewController's view property. You should create another UIView in IB, and load it use `UIView * view = (UIView *)[[[NSBundle mainBundle] loadNibNamed:@"YourNibName" owner:(nib owner) options:nil] objectAtIndex:0];` method. Then assign `scrollView.contentSize` with `view.bounds.size`. – KudoCC Jan 16 '14 at 02:20