Add a Scrollview to Existing View

Add a ScrollView to existing View

UPDATE: There's an easier way to do this which I didn't know when I posted this answer

1) Go to the viewcontroller on the storyboard and click on something in it and press Command + A. This will select all the elements in the view.

2) Go to Editor -> Embed In -> Scroll View. This will embed all the labels, views, etc into a scrollView.

3) Select the scrollview that you just embedded. Press Control and drag it to the respective class file of the viewcontroller and create an outlet scrollView.

4) Add this to your viewDidLoad()

scrollView.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height+100)

Make sure the height for the contentSize is more than the size of your view.

ADDING SCROLLVIEW MANUALLY

1) Drag and drop a scrollview from the Object Library onto your viewcontroller in the storyboard and create an outlet to your program as 'scrollView'.

2) Click on your viewcontroller and go to the size inspector and note down the width and height.

3) Click on your scrollview and set the width and height the same as the viewcontroller. and set the X and Y values to 0

4) Click on the scrollView and drag it a little bit to the side

5) Press Command+A to select all the elements including scrollView. Press Command and click on the scrollView to deselect the ScrollView

6)You will have all the elements except the scrollView selected now. Now click and drag them into the scrollView.

Moving into scrollView

7) Now click on the scrollView and set the X and Y values to 0 from the Size Inspector.

8) Add this to your viewDidLoad()

scrollView.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height+100)

Make sure the height for the contentSize is more than the size of your view.

That should create a scrollView for your view. Some of the elements might be in a slightly different position. You can easily fix them by moving them on your storyBoard.

How to add a scroll view to the existing view controller?

Try this to make the main view a scrollview:

- (void)loadView {
// create and configure the scrollview
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.view = scrollView;
}

loadView is the method where the subclassed View Controllers should configure their main view, it is called automatically before the VC loads and shouldn't be called directly.

After adding all the elements to the scrollview you want to change the content size so it actually scrolls with something like this:

// this for a horizontal scroll
[scrollView setContetSize:CGSizeMake(CGRectGetMaxX(lastElementOnTheRight), scrollView.frame.size.height)];

// this for a vertical scroll
[scrollView setContentSize:CGSizeMake(scrolView.frame.size, CGRectGetMaxY(lastElementOnTheBottom))];

Adding a UIScrollView to an existing view with lots of objects... how to easily make whole view scrollable?

I don't know how your views are created, but the easiest thing would be to change the class of the main view from UIView to UIScrollView.

How to add Scroll View in Swift 5?

It works 100% when I Disabling the content layout guides in properties

Sample Image



Related Topics



Leave a reply



Submit