Change UIScrollView Content size
Asked Answered
P

5

6

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));
}
Photoplay answered 9/5, 2016 at 10:56 Comment(0)
P
8

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

Photoplay answered 10/5, 2016 at 11:44 Comment(0)
A
1

Hey @Dim you can try this-

yourScrollView.contentSize = CGSizeMake(320, 568)

And you can change size 568 to whatever you want.

Aubree answered 9/5, 2016 at 11:47 Comment(5)
Hey @Mohd, I tried added scrlMain.contentSize = CGSizeMake(320, 790) and in the viewDidLoad. But I have the same problem.Photoplay
For me not. I move UIScrollView to down, but it does not fix. It moves back. If add this code in viewDidAppear, UIScrollView can fix. But I can't use viewDidAppear.Photoplay
Can you share your screen using <Skype or TeamViewer>Aubree
Yes, I can. How I can do it?Photoplay
I wrote you in the Skype. If you didn't get my request, write me your login please.Photoplay
S
0

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

Skite answered 9/5, 2016 at 13:18 Comment(1)
I added code: scrlMain.userInteractionEnabled = true; in the viewDidLoad. But I have the same problem. In the Storyboard ViewController has 570, UIScrollController has 570.Photoplay
B
0
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
    }
}
Bazemore answered 10/8, 2017 at 10:37 Comment(1)
viewDidLayoutSubviews is always called on the main thread - passing this work to the main thread from the main thread is redundant.Accouter
R
0

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()
Rein answered 30/3, 2022 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.