I have a UIViewController, and I want to add a UIScrollView to it (enable scroll support), is it possible?
I know it is possible, if you have a UIScrollView to add a UIViewController to it, but I’m interested also if reverse was true, if I cann add a UIScrollView to an existing UIViewController, such that I get scrolling feature.
Edit
I think I have found an answer: Adding a UIViewController to UIScrollView
An
UIViewControllerhas aviewproperty. So, you can add aUIScrollViewto itsview. In other words, you can add the scroll view to the view hierarchy.This is can achieved by code or through XIB. In addition, you can register the view controller as the delegate for your scroll view. In this way, you can implement methods for performing different functionalities. See
UIScrollViewDelegateprotocol.You could also override
loadViewmethod forUIViewControllerclass and set the scroll view as the main view for the controller you are considering.Edit
I created a little sample for you. Here, you have a scroll view as a child of the view of a
UIViewController. The scroll view has two views as children:view1(blue color) andview2(green color).Here, I suppose you can scroll in only one direction: horizontally or vertically. In the following, if you scroll horizontally, you can see that the scroll view works as expected.
If you need to scroll only vertically you can change as follows:
Obviously, you need to rearrange the position of
view1andview2.P.S. Here I’m using ARC. If you don’t use ARC, you need to explicitly
releasealloc-init objects.