Why does changing a UIScrollView frame change the scope of its subzones?

It puzzles me. I want to change the UIScrollView frame when changing orientation:

if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){

            self.myScrollView.frame = CGRectMake(40, 40, 984, 200);
            for (UIView* view in [self.myScrollView subviews]){
                NSLog(@"subview x: %f", view.frame.origin.x);
            }

        }
        else{

            self.myScrollView.frame = CGRectMake(40, 40, 728, 200);
            for (UIView* view in [self.myScrollView subviews]){
                NSLog(@"subview x portrait: %f", view.frame.origin.x);
            }
        }

Here are the results. Please note that all views move 156 pixels to the left, although all I did was change the width of the parent scroll (256 pixels less in portrait mode):

    subview x: 0.000000
    subview x: 128.000000
    subview x: 256.000000
    subview x: 384.000000

    subview x portrait: -156.000000
    subview x portrait: -28.000000
    subview x portrait: 100.000000
    subview x portrait: 228.000000

Why?? And how to prevent it?

+3
source share
2 answers

You probably have the wrong autorisize flags set in subviews. You can also disable UIScrollView autoresizesSubviews (ininterface builder or manually) to simply disable this behavior.

+11
source

, ?

myScrollview.resizesSubviews = ;

uiscrollview, layoutSubviews ;)

0

Source: https://habr.com/ru/post/1769595/


All Articles