UIScrollView setContentSize doesn't work
Solution 1
I had the same issue until I realized I hadn't set the UIScrollView delegate. Also, if you're using auto layout, you need to wait for the layout to be applied to the UIScrollView. This means you should set the Content Size in "viewDidLayoutSubviews".
Solution 2
In viewDidLoad method, frame may not be correct, move the code to this method:
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Your code
}
Solution 3
Use this code. ScrollView setContentSize should be called async in main thread.
Swift:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
DispatchQueue.main.async {
var contentRect = CGRect.zero
for view in self.scrollView.subviews {
contentRect = contentRect.union(view.frame)
}
self.scrollView.contentSize = contentRect.size
}
}
Objective C:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
dispatch_async(dispatch_get_main_queue(), ^ {
CGRect contentRect = CGRectZero;
for(UIView *view in scrollView.subviews)
contentRect = CGRectUnion(contentRect,view.frame);
scrollView.contentSize = contentRect.size;
});
}
Solution 4
Okay, there is absolutely nothing wrong with your code. So some suggestions:
Make sure you added to your .h file like so:
@interface yourViewController : UIViewController <UIScrollViewDelegate> { }
Try moving this snippet of code to the "viewWillLoad" method:
- (void)viewWillAppear:(BOOL)animated { self.scrollView.delegate = self; [self.scrollView setScrollEnabled:YES]; [self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)]; }
anthoprotic
Updated on June 06, 2020Comments
-
anthoprotic almost 4 years
I have a really big problem with UIScrollView. To make sure it wasn't my actual project, I created a NEW project, iPhone only, for iOS 6 and 7. I disabled autolayout.
- I created a Tab Bar project
- I created a view controller, embedded it in a navigation controller and connected it as Tab bar item #0
- I put a UIScrollView in my view.
- I connected the scrollview in the custom UIViewController created for this view.
- I synthesized my IBOutlet and set the delegate to self. I also implemented delegate in .h
Here is my code (ViewDidLoad):
self.scrollView.delegate = self; [self.scrollView setScrollEnabled:YES]; [self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)]; NSLog(@"contentSize width %f", self.scrollView.contentSize.width); NSLog(@"contentSize height %f", self.scrollView.contentSize.height); NSLog(@"%@", NSStringFromCGAffineTransform(self.scrollView.transform));
It returns me "contentSize width 20000.000000", "contentSize height 0.000000 " and "[1, 0, 0, 1, 0, 0]". It scrolls left-right where it should scroll up-down
Please help!
-
anthoprotic over 10 yearsDoesn't change a thing, unfortunately.
-
quaertym over 10 yearsAlso check for orientation
-
anthoprotic over 10 yearsWhat do you mean? My scrollview takes all the space on my ViewController
-
quaertym over 10 yearsAre you sure that self.view is not nil and self.view.frame is correct?
-
anthoprotic over 10 yearsYes, I NSLogged it and self.view isn't nil. And I tried [self.scrollView setContentSize:CGSizeMake(1500, 20000)]; doesn't change a thing.
-
anthoprotic over 10 yearsI know right! That's what really frustrating. I tried your way, but viewWillLoad doesn't get called. Never.
-
anthoprotic over 10 yearsI created a completely NEW project only to test my UIScrollView code (like above). Nothing can be corrupted. Can it be a setting in my .plist that still thinks autolayout it activated? I'm using Xcode 5.0.2
-
quaertym over 10 yearsDid you disable autolayout for all of your view controllers in the storyboard?
-
JustAnotherCoder over 10 yearsSorry I didn't mean viewWillLoad, try viewWillAppear
-
hatfinch over 10 yearsNB there is no "-viewWillAppear" method, only "-viewWillAppear:(BOOL)animated".
-
anthoprotic over 10 yearsI disabled autolayout in the view inspector, which disables autolayout all over the app I think. I tried putting the code inside - (void) viewDidAppear:(BOOL)animated, didn't work too.
-
anthoprotic over 10 yearsSorry, I meant - (void) viewWillAppear:(BOOL)animated
-
anthoprotic over 10 yearsI'm now starting to get desperate about this. Could someone create a new project with only 1 view controller and a UIScrollView that scroll up and down with autolayout disabled? If I can't get your project to work on my computer, I'll know what's up.
-
Code Hunter over 10 yearsPlease try this link stackoverflow.com/questions/12846351/…
-
Eloy almost 10 yearsThanks! I didn't get my scrollview to work either, until I read your answer, and set the content size in viewDidLayoutSubviews.
-
Alyoshak almost 10 yearsSetting the contentSize property in viewDidLayoutSubviews worked for me. Had nothing to do with auto layout.
-
fischgeek almost 9 yearsOh my gosh. After days of endless searching and messing with all kinds of solutions to no avail, this post was the answer. Thank you so much.