UIScrollView contentSize not working

前端 未结 15 1612
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 06:18

I put a UIScrollView in my nib\'s view, and linked it to a an IBOutlet property.

Now, when I do this in my viewDidLoad

15条回答
  •  星月不相逢
    2020-11-28 07:17

    A SUPER easy way to use AutoLayout with UIScrollViews inside Interface Builder:

    Step 1: Create a UIScrollView

    Step 2: Create a UIView that is a child of your scroll view like so:

    -UIScrollView
    ---UIView
    -----Your other content
    

    (We'll call this one contentView).

    Step 3: In the size inspector, give this view a height and width (say, 320x700).

    Step 4 (using AutoLayout): Create unambiguous constraints from your contentView to its superview (the UIScrollView): connect the 4 edges (top, leading, trailing, bottom), then give it a defined width and height that you want it to scroll too.

    For example: If your scroll view spans the entire screen, you could give your content view a width of [device width] and a height of 600; it will then set the content size of the UIScrollView to match.

    OR:

    Step 4 (not using AutoLayout): Connect both of these new controls to your view controller using IB (ctrl+drag from each control to your view controller's .h @implementation). Let's assume each is called scrollView and contentView, respectively. It should look like this:

    @interface YourViewController : UIViewController
    
    @property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
    @property (strong, nonatomic) IBOutlet UIView *contentView;
    
    @end
    

    Step 5 (not using AutoLayout): In the view controller's .h file add (actually, override) the following method:

    -(void)viewDidLayoutSubviews
    {
        [super viewDidLayoutSubviews];
        self.scrollView.contentSize = self.contentView.frame.size;
    }
    

提交回复
热议问题