Storyboard UIScrollView contentSize?

2019-01-22 20:44发布

I feel like I have touched on every single possible cause for stopping this, but I have a UIScrollView in my Storyboard hooked up with an outlet and in the viewDidLoad I set the contentSize so that I can scroll (yes bigger than my frame size)!

However, whatever I change, I just can't scroll! I have a couple of textfields in my scrollview and bouncing enabled so I can see that when testing its moves up and down with my subviews in it but whatever I set the contentSize to I just can't scroll.

Anything I might be missing/should check? Is this a known issue with UIScrollView being used in a storyboard?

Whats even stranger is, I can do something like this: [scrollView setBackgroundColor:[UIColor blueColor]]; and I have a blue scroll view! But setting content size fails.

Edit

My only code (otherwise scrollview is just dropped into storyboard view controller):

-(void)viewDidAppear:(BOOL)animated
{
    [scrollView setContentSize:CGSizeMake(320, 640)];
}

Logged frame, comes out as expected:

width: 320.00
height: 504.00

Edit 2

Turns out that removing any subviews of the scroll view in my storyboard lets it scroll just fine. If I add any subview to it at all via the storyboard, even a blank brand new UIButton it just won't apply the contentSize/allow scrolling.

7条回答
叛逆
2楼-- · 2019-01-22 20:59

The issue is most probably with Auto Layout. UIScrollView needs special attention when using AutoLayout.

Quick-fix - bind one of the scroll's subviews to the top AND bottom space of it's superview (the scroll view).

Long story: Questions on SO: UIScrollView not scrolling regardless of large contentSize, UIScrollView will not scroll, even after content size set, UIScrollView doesn't use autolayout constraints

Apple's Documentation: https://developer.apple.com/library/ios/technotes/tn2154/_index.html

查看更多
smile是对你的礼貌
3楼-- · 2019-01-22 21:01

use ViewDidLayoutSubview

- (void)viewDidLayoutSubviews
{
    [_scrollView setContentSize:CGSizeMake(320, 500)];
}

UIViewController's method invoke sequence is as below

  • awakeFromNib
  • viewDidLoad
  • viewWillAppear
  • viewWillLayoutSubviews
  • viewDidLayoutSubviews
  • viewDidAppear
查看更多
闹够了就滚
4楼-- · 2019-01-22 21:01

Check these

  • User Interaction enabled
  • Outlet connected
  • Included contentsize greater than bounds
  • scrolling Enabled

eg

scrollView.contentSize = CGSizeMake(320, 640);

My storyboard looks like this for scrollview [working]

enter image description here

查看更多
唯我独甜
5楼-- · 2019-01-22 21:03

viewDidLoad is not a good place to put code that relies on frame sizes of IB objects. If you log the contentSize of your scroll view in viewDidLoad, you will see that it's (0,0). Move the code (where you set the content size) to viewDidAppear, and it will work properly.

查看更多
你好瞎i
6楼-- · 2019-01-22 21:06

I had exactly the same line of code in viewDidAppear and it did not work

Moved it to viewDidLayoutSubviews and it worked correctly.

[scrollView setContentSize:CGSizeMake(320, 500)];

Thanks trick14 for the answer.

查看更多
\"骚年 ilove
7楼-- · 2019-01-22 21:16

Trip14's answer worked for me. In swift I coded it as:

override func viewDidLayoutSubviews() {

          (self.view as! UIScrollView).contentSize = CGSizeMake(600, 600)
        }
查看更多
登录 后发表回答