zyl1024
zyl1024

Reputation: 700

Why can't I add things directly to UIScrollView?

I'm new to iOS programming and maybe I don't fully understand the UIScrollView. From what I've researched, all methods of making a scroll view include embed a UIView or UIImageView into a UIScrollView. However, I don't know whether there is a way to directly drag objects (UILabel, UIButton...) to UIScrollView. And if the size of UIScrollView is set to be larger than the screen, then everything on the UIScrollView is scrollable. Again, that's all my thoughts and I don't find any post to confirm or deny this. I think if it's possible, then it's the easiest way to set up a scrollable screen.

If that's not possible, can anyone point to an easy way to scroll a long list of UIButtons (only vertically)? I found many tutorials but most are for scrolling a UIImageView and I can't adopt the method to a UIView containing the list of UIButtons. Thanks very much for helping me.

I'm in iOS 5 and storyboard mode.

Upvotes: 0

Views: 178

Answers (4)

Artem Shmatkov
Artem Shmatkov

Reputation: 1433

Because UIImageView, UILabel, UIButton is a descendants of UIView you can add instances of those classes usin addSubview: method.

Also you need to properly setup frames of your views.

If you want scrollable content size larger than screen size so you need to set scrollView.contentSize to needed value.

Upvotes: 2

Hermann Klecker
Hermann Klecker

Reputation: 14068

In storyboard I suggest not adding UILabel etc. directly to the scroll view. It can be done but I found it easier having only one view (or very fiew) directly in the containing view of a scroll view. Especially when a single view is larger than the screen.

That's why I suggest to crate one plain UIView object. Use freestyle size for drawing the UIView in IB. That should be your only direct subview to the scroll view. Within that view you can easily arrange and rearrange all your ui items. But make sure that you do not use autolayout and that your view does not resize the subvies and the scroll view does not autoresize the subviews. Otherwise your properly layouted view, that is supposed to be larger than the screen, could be quiezed into the screens (respectively available parts of the window) dimensions.

Upvotes: 0

junkor
junkor

Reputation: 373

Of cause. Case UILabel, UIButton ... is subClass of UIView. You can add UIView to ScrollView ,you could add them,too.so do UIScrollView,for it comes from UIView,too.

Use View to contain the button let you manage the buttons easily.

you should calculate all the frames of the buttons.if they have some same layout feature, you would like to use tableView.

Upvotes: 1

Fogmeister
Fogmeister

Reputation: 77641

Scrollview is great for documents, images, webviews, etc...

But for laying out UI it's a bit of a pain.

If you're just laying out buttons vertically then you might want to look at using UITableView to manage the layout for you.

UITableView is good for more than just displaying lists of data.

If you want to stick with scrollView then you can create a new UIView nib file and make that the full size you want it to be. Drag all your buttons into it and then put THIS UIView into your scrollView using code.

That way you've done the layout with IB dragging the buttons but still use the code to put all of those buttons etc into the scrollview.

Upvotes: 0

Related Questions