Cocoa touch UIViewController使用代码更改方向

Cocoa touch UIViewController使用代码更改方向,cocoa-touch,uiviewcontroller,uiinterfaceorientation,interface-orientation,Cocoa Touch,Uiviewcontroller,Uiinterfaceorientation,Interface Orientation,为了获得更好的用户体验,我的代码中包含以下内容: if (allowLandscape) { return UIInterfaceOrientationIsLandscape(toInterfaceOrientation) || toInterfaceOrientation == UIInterfaceOrientationPortrait; } else { return toInterfaceOrientation == UIInterfaceOrientationPortr

为了获得更好的用户体验,我的代码中包含以下内容:

if (allowLandscape) {
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation) || toInterfaceOrientation == UIInterfaceOrientationPortrait;
}
else {
    return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}
所以,如果allowLandscape设置为YES,用户可以在所有典型的设备方向上使用该应用程序。但是allowLandscape可以随时设置为NO,我想将UIViewController旋转回纵向

以下代码仅在用户手动更改设备方向时有效。因此,如果allowLandscape是肯定的,他可以旋转设备,接口方向也会随之改变。但是如果他保持横向,并且allowLandscape将设置为否,他将保持横向,直到他将iPhone移动到纵向-现在,如果allowLandscape仍然为否,则无法更改纵向

我想完成的是调用一些方法,强制ViewController在可能的情况下使用动画刷新其方向

调用该方法:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
只更改状态栏,但界面仍保持在以前的方向。我不想使用任何未记录的API。

以下是解决方案:

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

通过添加此代码,视图控制器再次询问,它应该在哪个方向显示视图。

就我个人而言,我不会实现已经内置到操作系统中的功能。嗯,你指的是什么功能?如何禁用横向方向并通过动画代码将方向翻转为纵向?我说的是一个UIViewController。也许我在重读后误解了你的问题。用户可以通过“任务”菜单双击“主页”按钮,然后向右滑动来锁定和解锁方向。