How can I modify an existing UIViewController to allow scrolling?
I have already built a UIViewController
subclass with a bunch of controls in it, and just realized that if I rotate the iPhone, half of the controls become invisible. So, I would like to somehow make the UIViewController's
UIView
scrollable so that when the device (or the Simulator) rotates, the user can scroll the view to see all the controls.
I was hoping to do this all in Interf开发者_如何学Goace Builder. I tried to change the class of the view from UIView
to UIScrollView
in the Class Identity editor, but nothing scrolls. The base class of my view controller is a simple UIViewController
<UIScrollViewDelegate>
.
Is there an easy way to make the main view in my view controller scrollable without having to recreate the whole thing in IB?
For people who are using storyboard, this is quite easy to do
- in Document Outline select the topmost
View
of the UIViewController - then in Identity Inspecter, under Custom Class, for class enter
UIScrollView
That's it.
Add a UIScrollView
and make all your controls and widgets and labels subviews of the scroll view by dragging them from wherever they are "into" or "inside" the scroll view. This makes them subviews of the scroll view, which is what you want.
You can do this as follows:
- Open your nib file and Create a UIScrollView object under your UIView Object.
- Move all your controls onto the UIScrollView
- Open your header file (.h) and add a new property for the scroll view:
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
- Open your implementation file (.m) and insert the following:
@synthesize scrollView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Enable scrolling for portrait
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, 600);
}
- Go back to your nib file and wire up the scrollView on the file's owner to your UIScrollView object.
The real trick here is setting the scrollView.contentSize. Once this is set, the scrolling should occur.
If you wanted to enable scrolling for when the device is in portrait orientation, you could use this:
// Enable scrolling for landscape orientation
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
This should give you some frame of reference.
Flea
It would be helpful to understand what your interface is displaying, but I'd suggest one of the following:
- Don't allow rotation by returning NO from shouldAutorotateToInterfaceOrientation
- Use Interface Builder to adjust your springs and struts so that all of your interface elements fit in landscape view
- Add a new UIScrollView in Interface Builder and drag your UIView into it, then re-assign the view property of your File's Owner to the scroll view.
I was able to do this by adding a scroll view to the view in IB, making the view controller a UIScrollViewDelegate, hooking up the scrollview to a UIScrollView object in IB, hooking the delegate up to file owner, and adding this line in the viewDidLoad:
- (void) viewDidLoad {
scrollView.contentSize = CGSizeMake(1280, 960);
}
Scrolling behavior is only invoked if the contentSize
of a UIScrollView
is larger than its bounds.
Add a Scrollbar into Xib file and then drag & drop controls in to the Scrollbar. Inside load view
use
scrollView.contentSize = CGSizeMake(CONTENT_WIDTH, CONTENT_HEIGHT);
精彩评论