How do I force a specific UIInterfaceOrientation on an individual view in a UINavigationController?

后端 未结 8 883
闹比i
闹比i 2020-12-14 17:27

Okay, so here\'s the situation:

I have an app in which I only want ONE specific view in a UINavigationController to have a landscape orientation. This view is a UIIm

相关标签:
8条回答
  • 2020-12-14 18:10

    You can try to force Device to rotate to necessary orientation - but you need to handle it manually (in addition to overriding UIViewController orientation handling methods).

    To rotate device you can use next methods:

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    

    But in it may not work in all situations...

    Also available undocumented approach:

    [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:")
                                           withObject:(__bridge id)((void*)UIInterfaceOrientationLandscapeLeft)];
    
    0 讨论(0)
  • 2020-12-14 18:12

    I think you can embed this view inside a view controller and overwrite the ShouldRotateToInterfaceOrientation method. Good luck!

    0 讨论(0)
  • 2020-12-14 18:15

    Just override this UIViewController method to only return true for landscape like so and the iphone will be forced to rotate to that device orientation, since it has no other option.

    - (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
    
    0 讨论(0)
  • 2020-12-14 18:21

    This might be what your looking for.

    // Rotates the view.
    CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
    self.view.transform = transform;
    
    // Repositions and resizes the view.
    CGRect contentRect = CGRectMake(-80, 80, 480, 320);
    self.view.bounds = contentRect;
    

    from http://www.iphonedevsdk.com/forum/iphone-sdk-development/1394-landscape-uiviewcontroller-uiview-rotation.html

    0 讨论(0)
  • 2020-12-14 18:21

    The UINavigationController overrides the contain UIViewController orientation settings, so you have to create a custom subclass of UINavigationController with the following for 5.1:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        if ([[self topViewController] isKindOfClass:[SigCaptureViewController class]]) {
            return UIInterfaceOrientationIsLandscape(interfaceOrientation);
        } else {
            return UIInterfaceOrientationIsPortrait(interfaceOrientation);
        }
    }
    

    For 6.0 and above you need:

    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations {
        if ([[self topViewController] isKindOfClass:[EXTRASigCaptureViewController class]]) {
            return UIInterfaceOrientationMaskLandscape;
        } else {
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    

    What I haven't figured out is how to make the force the UINavigationController to rotate. calling [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO] causes the status bar to rotate but doesn't cause the view to rotate.

    0 讨论(0)
  • 2020-12-14 18:22

    To use a View in only landscape, I have the following in the ViewController:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
    }
    
    0 讨论(0)
提交回复
热议问题