Overriding layoutSubviews when rotating a UIView

When you rotate the scroll view inside the UIView, the scroll view is not positioned correctly using the default autoresist behavior.

Thus, when a rotation occurs, willRotateToInterfaceOrientationI call in [self.view setNeedsLayout];, and my method is layoutSubviewsas follows:

- (void)layoutSubviews {
    NSLog(@"here in layoutSubviews");
}

But by setting a breakpoint on the method, it never introduces the method.

Do I need to do something to make it work?

Thank.

+3
source share
5 answers

willRotateToInterfaceOrientation , UIView - . didRotateFromInterfaceOrientation.

, scrollView (, UIAnimation) willRotateToInterfaceOrientation, didRotateFromInterfaceOrientation ( , , ).

:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
self.myScroll.hidden = YES;
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView commitAnimations];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
self.myScroll.hidden = NO;
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView commitAnimations];
}

, , UIInterfaceOrientationIsPortrait () UIInterfaceOrientationIsLandscape ().

+3

[someScrollView setNeedsLayout] , . , , . , someScrollView.autoresizingMask. , layoutSubviews.

+2

, , viewAnimateRotationToInterfaceOrientation: duration: ( ):

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                        duration:(NSTimeInterval)duration {
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        boardView.frame = CGRectMake(0, 0, 320, 320);
        buttonsView.frame = CGRectMake(0, 320, 320, 140);
    } else {
        boardView.frame = CGRectMake(0, 0, 300, 300);
        buttonsView.frame = CGRectMake(300, 0, 180, 300);        
    }

}

0

, ViewController layoutSubviews.

When you call [self.view setNeedsLayout];, he simply calls a method layoutSubviewsto represent the type of controller: [self.view layoutSubviews].

To do this, you need to subclass UIScrollview.

0
source

You must override willAnimate ... and set a new frame for your view. Layoutsubviews should be called automatically during rotation.

0
source

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


All Articles