János
János

Reputation: 35112

Prevent UIScrollView scroll horizontally if auto layout is on

I have this scrollview with autolayout. Scroll view has the blue color. In landscape mode it looks good.

enter image description here

But in portrait mode instead of let horizontally scroll I would reduce the width of the view with orange color to keep 20px distance from right edge too.

How? I set up 20px constrain on right side also, but the contentSize not get updated when rotating. I know that I am not allowed to set contentSize programmatically in case of autolayout.

enter image description here

Here you can see the constrains I set up:

enter image description here

Upvotes: 2

Views: 850

Answers (1)

János
János

Reputation: 35112

I figured out. Instead of setting the right constrains, I created constrain for the width of view, created an IBOutlet for the constrain, and in code I set the constant of the constrain.

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
    if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {

        //self.scrollView.contentSize = CGSizeMake(944, 1600);
        _viewWidthConstrain.constant = 944;

    } else {

        //self.scrollView.contentSize = CGSizeMake(500, 1600);
        _viewWidthConstrain.constant = 500;
    }
}

And here is the result:

enter image description here

Upvotes: 1

Related Questions