UIScrollView won't scroll (Storyboards)

2020-03-17 13:52发布

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.

2条回答
地球回转人心会变
2楼-- · 2020-03-17 14:36

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.

查看更多
家丑人穷心不美
3楼-- · 2020-03-17 14:42

Just add the following method:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [self.scrollView setContentSize:CGSizeMake(320, 1700)];
}

This is working perfectly for me.

查看更多
登录 后发表回答