Disabling autorotate for a single UIView

前端 未结 4 1819
野趣味
野趣味 2020-12-17 00:41

Okay, this seems like it should be relatively simple, but I\'ve been Googling for the better part of an hour, and can\'t seem to find what I need.

I have a view cont

4条回答
  •  一个人的身影
    2020-12-17 01:03

    There is a 'proper' way: Allow the framework to rotate the interface (other wise you'll run in to problems when popping up another view controller) but override the animation of your view.

    I have a fullscreen view that contains everything I don't want to rotate, called _fixedView. In storyboard I made sure all autosizing (those red arrows at the 'Size Inspector') are turned off, so the view will always be in the center of the screen.

    Then in my view controller I have this override:

    -(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        [UIView animateWithDuration:duration
                         animations:^{
            if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
                _fixedView.transform = CGAffineTransformMakeRotation(M_PI_2);
            } else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
                _fixedView.transform = CGAffineTransformMakeRotation(-M_PI_2);
            } else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
                _fixedView.transform = CGAffineTransformMakeRotation(M_PI);
            } else {
                _fixedView.transform = CGAffineTransformIdentity;
            }}];
    }
    

    This way everything in _fixedView is completely fixed (how is that for descriptive names, eh?) While the status bar and my toolbar rotate normally.

提交回复
热议问题