'Change UIScrollView Content size

I have ViewController with UIScrollView, some UIImages and UIButtons. In the Storyboard I set the size of UIScrollView to: width: 320, height: 570

I want to change Contentsize of my UIScrollView when the ViewController was loaded.

I added side in the viewDidLoad:

        scrlMain.contentSize = CGSize(width: 320,height: 790)

And also I added code in the viewDidLayoutSubviews:

        self.scrlMain.translatesAutoresizingMaskIntoConstraints = true

But my UIScrollView is still the last size = 570. I can't use viewDidAppear because, when I press the button, it need it to change color. When it has changed color, my UIScrollView moves to up.

What did I do wrong?

All code:

    override func viewDidLoad() {
    super.viewDidLoad()
    scrlMain.delegate = self
    scrlMain.contentSize = CGSizeMake(320, 790)
    }
override func viewDidLayoutSubviews() {
    self.scrlMain.translatesAutoresizingMaskIntoConstraints = true
    self.scrlMain.contentSize = CGSize(width: self.scrlMain.frame.width, height: 60.0 + 5 * (self.scrlMain.frame.width / 16.0 * 5.0));
}


Solution 1:[1]

there was an error in the this function:

override func viewDidLayoutSubviews() {
self.scrlMain.translatesAutoresizingMaskIntoConstraints = true
self.scrlMain.contentSize = CGSize(width: self.scrlMain.frame.width, height: 60.0 + 5 * (self.scrlMain.frame.width / 16.0 * 5.0));
}

I delete this code:

self.scrlMain.contentSize = CGSize(width: self.scrlMain.frame.width, height: 60.0 + 5 * (self.scrlMain.frame.width / 16.0 * 5.0));

And all was right

Solution 2:[2]

Hey @Dim you can try this-

yourScrollView.contentSize = CGSizeMake(320, 568)

And you can change size 568 to whatever you want.

Solution 3:[3]

Try this:

yourScrollView.userInteractionEnabled = YES

and you please maintain the difference between your screen size and your scrollview content size... so that it can be visible properly

suppose yourScrollViewSize greatertThan yourScreenSize

Solution 4:[4]

override func viewDidLayoutSubviews() {

    DispatchQueue.main.async {
    self.scrollview.translatesAutoresizingMaskIntoConstraints = true
    self.scrollview.contentSize = CGSize(width:0, height:self.scrollview.frame.height*2) // whatever hight you want
    }
}

Solution 5:[5]

It is also possible to change the content size from within the page without running ViewDidLoad again.

Just add

scrollView.layoutIfNeeded()

after you set the new contentSize

Example when you change the content size (viewImages) within the UIscrollView (scrollViewImages)

viewImages.frame.size = CGSize(width: width, height: 128)
viewImages.translatesAutoresizingMaskIntoConstraints = true
viewImages.setNeedsLayout()
scrollViewImages.setNeedsLayout()
scrollViewImages.layoutIfNeeded()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vaibhav Saran
Solution 2 Swift Developer
Solution 3 Rama Krishna
Solution 4
Solution 5