UIScrollView contentSize not working

2019-01-06 13:02发布

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 method, it seems to have no effect on the contentSize:

self.sv.backgroundColor = [UIColor yellowColor]; // this works
CGSize size =  CGSizeMake(1000.0, 1000.0); 
[self.sv setContentSize:size]; // this does not

It behaves as if the contentSize was the same as the frame. What's going on? This started working when I turned off AutoLayout. Why?

14条回答
Explosion°爆炸
2楼-- · 2019-01-06 13:17

I used to do set up the uiscrollview programmatically UNTIL I watched the following wonderful tutorial, step by step how to get uiscrollview and uiview to work: https://www.youtube.com/watch?v=PgeNPRBrB18

After watching the video you will start liking Interface Builder I am sure.

Vote up

查看更多
小情绪 Triste *
3楼-- · 2019-01-06 13:18

Try this out...
add all constraints like you do for UIView (See screenShot of my ViewControler in Storyboard)
enter image description here
Now trick begins. select your last object and select its bottom constraint. (See above screenShot, Instagram button's Bottom Constraint(Yellow line)) and Change the Constant in Size Inspector like in bellow screenshot.

enter image description here

i require Constant=8 but you can change as per your requirements. this Constant is the Space between That Orange Button's Bottom and the scrollView.

EDIT

Make Sure about your view's hierarchy .

0) ViewController.view (optional)
1) UIScrollView
2) UIView (Rename as "contentView")
3) UIView (this view is your content that will make scrollView scroll)

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-06 13:23

Setting the contentSize in viewDidAppear is critical.

But I also had a variation of what worked in the 3.5 inch screen, and the 4 inch screen. The 4 inch screen worked, the older one does not. Both iOS 7. Bizarre is an understatement!

查看更多
SAY GOODBYE
5楼-- · 2019-01-06 13:25

You can use this lines of code into your *.m file's

- (void)viewDidLoad{
   [scroll setContentSize:CGSizeMake(320, 800)]   ;
   [scroll setScrollEnabled:TRUE];
   [scroll setShowsVerticalScrollIndicator:NO];
   [scroll setShowsHorizontalScrollIndicator:YES];
}    

for this you need to take an IBOutlet property of UIScrollView into your *.h file this way:

IBOutlet UIScrollView *scroll;

And connect this from Storyboard.

Or,

You can use this method into your *.m file:

-(void)viewDidLayoutSubviews 
{
  [scroll setContentSize:CGSizeMake(320, self.view.frame.size.height* 1.5)];
   // this will pick height automatically from device's height and multiply it with 1.5  
}

This both solution works for me in xcode-5, xcode-6, xcode-6.1, xcode-6.2

查看更多
女痞
6楼-- · 2019-01-06 13:29

There are two problems here. (1) viewDidLoad is too soon; you have to wait until after layout has taken place. (2) If you want to use autolayout with a scrollview that comes from a nib, then either you must use constraints to completely describe the size of the contentSize (and then you don't set the contentSize in code at all), or, if you want to set it in code, you must prevent the constraints on the scrollview's subviews from dictating the contentSize. It sounds like you would like to do the latter. To do so, you need a UIView that acts as the sole top-level subview of the scrollview, and in code you must set it to not use autolayout, enabling its autoresizingMask and removing its other external constraints. I show an example of how to do that, here:

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/ch20p573scrollViewAutoLayout/ch20p573scrollViewAutoLayout/ViewController.m

But notice also the next example, which shows how to use constraints completely, instead of contentSize.

查看更多
Animai°情兽
7楼-- · 2019-01-06 13:30

Still not scrolling when dynamic height of labels exceeds view height.

I did what yuf's answer marked as correct above said to do (I added a content view to my scrollview and set the constraints leading, trailing, top bottom, and equal widths from the content view to the scroll view.) but still my view was not scrolling when the internal controls height exceeded the height of the scrollview.

enter image description here

Inside my content view I have an image and 3 labels below it. Each label adjusts their own height dependant on how much text is in them (they are set to word-wrap and numberoflines = 0 to achieve this).

The problem I had was my content view's height was not adjusting with the dynamic height of the labels when they exceeded the height of the scroll view/main view.

To fix this I worded out I needed to set the Bottom Space to Container constraint between my bottom label and the contentview and gave it a value of 40 (chosen arbitrarily to give it a nice margin at the bottom). This now means that my contentview adjusts its height so that there is a space between the bottom of the last label and itself and it scrolls perfectly!

Yay!

查看更多
登录 后发表回答