开发者

UIViewController change orientation with code

For better user experience I had the following in my code:

if (allowLandscape) {
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation) || toInterfaceOrientation == UIInterfaceOrientationPortrait;
}
else {
    return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}

So, if allowLandscape is set to YES, user can play with the app in all typical device orientations. But allowLandscape can be set to NO anytime, and I want to rotate the UIViewController back to portrait orientation.

The following code only works if the user manually change the device orientation. So if allowLandscape was YES, he could rotate the device and the interface orientation followed the orientation change. But if he stays in landscape orientation, and allowLandscape will be set to NO, he stays in the landscape orientation, until he moves iPhone to the portrait orient开发者_JS百科ation - now portrait orientation cannot be changed if the allowLandscape is still NO.

What I want to accomplish is to call some method, to force the ViewController to refresh it's orientation with animation if it's possible.

Calling the method:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

does only change the status bar, but the interface remains in the previous orientation. I don't want to use any undocumented APIs.


Here is the solution:

    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

By adding this code, view controller asks again, in which orientation should it present its view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜