UINavigationController and autorotation

后端 未结 7 1614
灰色年华
灰色年华 2020-12-13 19:46

I have a UIViewController that returns YES in shouldAutorotateToInterfaceOrientation: for UIDeviceOrientationPortrait and NO

7条回答
  •  暖寄归人
    2020-12-13 20:15

    I found a nice workaround for this problem. The clue is to support all orientations for all views in UINavigationController.

    I've got 2 views in controller. Root view is to support only LandscapeRight, and second supports both LandscapeRight and Portrait.

    Second view shouldAutorotateToInterfaceOrientation method looks as usual:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
               (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    

    The workaround itself is contained in Root view source Now the root view rotates in terms of code, but the user cant see it.

    //auxiliary function
    -(void) fixOrientation:(UIInterfaceOrientation)orientation
    {
        if (orientation == UIInterfaceOrientationPortrait)
            self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
        else if (orientation == UIInterfaceOrientationLandscapeRight)
            self.view.transform = CGAffineTransformMakeRotation(0);
    }
    
    -(void) viewWillAppear:(BOOL)animated
    {
        [self fixOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        [self fixOrientation:interfaceOrientation];
        //notice, that both orientations are accepted
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
               (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    //these two functions helps to avoid blinking
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        [UIView setAnimationsEnabled:NO]; // disable animations temporarily
    
    }
    
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        [UIView setAnimationsEnabled:YES]; // rotation finished, re-enable them
    }
    

提交回复
热议问题