Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView contentSize not working

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?

like image 648
cannyboy Avatar asked Oct 11 '12 18:10

cannyboy


People also ask

Why is ScrollView not scrolling?

To fix ScrollView Not scrolling with React Native, we wrap the content of the ScrollView with the ScrollView. to wrap ScrollView around the Text components that we render inside. As a result, we should see text inside and we can scroll up and down.

How do I make my stack view scrollable?

Add a UIStackView to the UIScrollView. Set the constraints: Leading , Trailing , Top & Bottom should be equal to the ones from UIScrollView. Set up an equal Width constraint between the UIStackView and UIScrollView . Set Axis = Vertical, Alignment = Fill, Distribution = Equal Spacing, and Spacing = 0 on the UIStackView.


2 Answers

I had the same problem. Auto Layout for UIScrollView is messed up.

Work around: Put everything in the UIScrollView into another UIView, and put that UIView as the only child of the UIScrollView. Then you can use Auto Layout.

If things near the end is messed up (the end of whichever direction your UIScrollView scrolls), change the constraint at the end to have the lowest possible priority.

like image 83
yuf Avatar answered Oct 03 '22 06:10

yuf


I tried viewWillLayoutSubviews to update scrollView's contentSize, it worked for me.

- (void)viewDidLayoutSubviews {   [self.bgScrollView setContentSize:CGSizeMake(320, self.view.frame.size.height* 1.5)]; } 

Apple Doc

-(void)viewDidLayoutSubviews 

Called to notify the view controller that its view has just laid out its subviews.

Discussion

When the bounds change for a view controller’s view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view’s subviews have been adjusted. Each subview is responsible for adjusting its own layout.

Your view controller can override this method to make changes after the view lays out its subviews. The default implementation of this method does nothing.

like image 42
HDdeveloper Avatar answered Oct 03 '22 06:10

HDdeveloper