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 Interface 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?

Answers


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:

  1. Open your nib file and Create a UIScrollView object under your UIView Object.
  2. Move all your controls onto the UIScrollView
  3. Open your header file (.h) and add a new property for the scroll view:

@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;

  1. 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);  

}
  1. 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


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.


It would be helpful to understand what your interface is displaying, but I'd suggest one of the following:

  1. Don't allow rotation by returning NO from shouldAutorotateToInterfaceOrientation
  2. Use Interface Builder to adjust your springs and struts so that all of your interface elements fit in landscape view
  3. 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.

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);

Need Your Help

iOS Widevine with Xamarin

hyperlink xamarin wrapper widevine

I am needing to use the widevine api in an ios app written in xamarin. From what I understand I need to make a wrapper for the library and such, but I have no idea where to start. Can anyone either

adding buttons in gridlayout programmatically and set width and heigth in percentage

c# android layout android-gridlayout

I have a method that creates Buttons with fix size of width and height. To create the Buttons I am using a layout.xaml template. The button template will be called by the command :