How do I notify system when supportedInterfaceOrientations changes?

一曲冷凌霜 提交于 2019-12-21 07:08:59

问题


My root view controller's implementation of supportedInterfaceOrientations almost always returns UIInterfaceOrientationMaskAll, however there is one edge case where it returns UIInterfaceOrientationMaskLandscape.

This is working, if the user rotates the device. However if the device is being held in portrait mode the supportedInterfaceOrientations method does not ever get called, unless the user manually rotates the device.

How can I programatically tell the system that the return value of this method has changed?

According to the documentation, it seems like I should be able to call [UIViewController attemptRotationToDeviceOrientation] however this does not have any effect (supportedInterfaceOrientations is never called and the screen does not rotate).

I found various workarounds others have posted to try and solve this problem, but none of them work in my tests. I suspect they may have worked in iOS 5.0, but not iOS 6.0.

I am returning YES in the root view controller's shouldAutorotate method.


回答1:


First of all, it might be useful if you used this if you want to present your UIViewController in Landscape mode.

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}

Also, A lot depends on with which controller is your UIViewController embedded in.

Eg, If its inside UINavigationController, then you might need to subclass that UINavigationController to override orientation methods like this.

subclassed UINavigationController (the top viewcontroller of the hierarchy will take control of the orientation.) needs to be set it as self.window.rootViewController.

- (BOOL)shouldAutorotate
 {
     return self.topViewController.shouldAutorotate;
 }
 - (NSUInteger)supportedInterfaceOrientations
 {
     return self.topViewController.supportedInterfaceOrientations;
 }

From iOS 6, it is given that UINavigationController won't ask its UIVIewControllers for orientation support. Hence we would need to subclass it.

Note :

The shouldAutorotate and supportedInterfaceOrientations method always get called for UINavigationController whenever Push operations are done.




回答2:


Quote from Apple's UIViewController Class Reference:

Note: At launch time, apps should always set up their interface in a portrait orientation. After the application:didFinishLaunchingWithOptions: method returns, the app uses the view controller rotation mechanism described above to rotate the views to the appropriate orientation prior to showing the window.

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

If the interface starts in portrait the autorotation should be able to handle the adjustment even if the user opens the app with the device on its side.

Update: I found this post that should help with rotations after launch. Apparently iOS 6 looks at the navigation controller to determine supported device orientations.

How to force a UIViewController to Portrait orientation in iOS 6




回答3:


You need to manually rotate it. You'll want to call the following logic in your view controller's viewWillAppear: method:

UIDeviceOrientation curDevOrientation = [[UIDevice currentDevice] orientation];
if (![self supportsOrientation:curDevOrientation]) {
    // We're going to rotate 90 degrees clockwise.  First figure out what that
    // means to the status bar.
    UIInterfaceOrientation newStatusBarOrientation;
    switch (curDevOrientation)  {
        case UIDeviceOrientationPortrait:
            newStatusBarOrientation = UIInterfaceOrientationLandscapeRight;
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            newStatusBarOrientation = UIInterfaceOrientationLandscapeLeft;
            break;
    }
    [[UIApplication sharedApplication] setStatusBarOrientation:newStatusBarOrientation animated:NO];

    // Now rotate the view 90 degrees clockwise.
    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI * 90.0 / 180.0);
    self.view.transform = transform;
}

That should rotate the particular view controller's view, whenever it appears.



来源:https://stackoverflow.com/questions/13150154/how-do-i-notify-system-when-supportedinterfaceorientations-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!