Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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));
}
like image 825
Dim Avatar asked May 09 '16 10:05

Dim


Video Answer


2 Answers

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

like image 181
Dim Avatar answered Oct 17 '22 18:10

Dim


Hey @Dim you can try this-

yourScrollView.contentSize = CGSizeMake(320, 568)

And you can change size 568 to whatever you want.

like image 44
Swift Developer Avatar answered Oct 17 '22 17:10

Swift Developer