UIScrollView won't scroll (Storyboards)
Solution 1
Two things turned out to be necessary:
1) Set height of scrollview to 480.
2) Add constraint to the bottommost field in the scrollview. The constraint was Pin >> Bottom Space to Superview.
Solution 2
Just add the following method:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self.scrollView setContentSize:CGSizeMake(320, 1700)];
}
This is working perfectly for me.
Casey Perkins
Updated on July 27, 2022Comments
-
Casey Perkins almost 2 years
I'm new to Storyboards; I've done some Interface Builder before and lots of manual UI positioning. I've searched this issue, tried the solutions found in the other relevant posts, to no avail.
I have a view controller with a UIScrollView added in Storyboards. The ScrollView outlet has been connected, the property synthesized. Scrolling Enabled is checked. Bounces is checked. Even then there was no indication that any scrolling would take place. When I checked Bounces Vertically, I could at least see the scrollable content, but it bounces back after I release. The frame size I found set at 320 and 521. I experimented with different heights but nothing helped. (What ought to be the size set in Storyboards that will accommodate the older and newer phone sizes?).
In viewDidLoad, I added
[scrollView setContentSize:CGSizeMake(320, 1000)];
A log statement afterwards confirms that this value has been accepted. But it didn't help either.
Someone in one post suggested adding:
- (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self.scrollView setContentSize:CGSizeMake(320, 808)]; }
This had the effect of crashing the program when the controller loaded.
Any help would be greatly appreciated!
Thanks.
-
Albin Stigo almost 10 yearsI also solved this by adding the right constraints to my UIScrollView.
-
Dimmy3 about 9 yearsThanks. Thanks. Thanks. And again, thanks. Number 2 was the problem.
-
Casey Perkins about 9 yearsI think #2 is the only one really needed. Not sure why I thought #1 was needed.
-
Markus about 8 yearsThat is the question: (void)viewDidLayoutSubviews. People (me included) write "self.scrollView setContentSize" in the wrong place, in (void)viewDidLoad or another places. Repeat: THAT IS THE QUESTION, the place (void)viewDidLayoutSubviews.
-
Nemanja over 7 yearsNumber two is the key, but basically you also put bottom layout constraint on superview and not set 480 for one.
-
user1872384 about 6 yearswhat is content size?