我已经搜索了几个小时的解决方案!
所以在实现必要的方法后,shouldAutorotate
不需要设置为YES
,因为它已经默认设置:
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
当需要显示方向与其他视图不同的UIViewController
时,我创建了一个带有以下实现的UIStoryboardSegue
:
#import "Showing.h"
@implementation Showing
- (void)perform{
NSLog(@"Showing");
UIViewController *sourceVC = self.sourceViewController;
UIViewController *presentingVC = self.destinationViewController;
[sourceVC.navigationController presentViewController:presentingVC
animated:YES
completion:nil];
}
@end
在 UIStoryboard
中,我使用这个 segue(显示)连接了视图:
这很重要,你正在使用
presentViewController:animated:completion:
而不是
pushViewController:animated:
否则方向将无法再次确定。
我一直在尝试类似的事情。
[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]
或者在UIViewController
中,方向应该改变的地方调用它,我还尝试在我的自定义UIStoryboardSegues
中,在presentingViewController
和dismissViewController
之前调用它:
[UIViewController attemptRotationToDeviceOrientation];
或者
NSNumber *numPortrait = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]
[[UIDevice currentDevice] setValue:numPortrait forKey:@"orientation"]
但是它们中没有一个起作用。当然,最后一个示例不应该是一个选择,因为如果苹果更改其 API 的任何内容,这可能会导致您的应用程序内部出现问题。
我还尝试使用AppDelegate
方法,并在查找实际visibleViewController
的正确UIInterfaceOrientation
后始终在此方法中确定方向,但有时切换方向时会崩溃。所以我仍然想知道为什么它如此复杂,似乎也没有任何文档正确地解释它。即使遵循这部分没有帮助我。