我已经寻找解决方案几个小时了!
所以在到处实现所需的方法之后。 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 连接起来(showing):
这很重要,你正在使用
presentViewController:animated:completion:
与否
pushViewController:animated:
否则将无法再次确定方向。
我一直在尝试像
[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
或这个在UIViewController 中的方向应该改变,我也尝试在presentingViewController 和dismissViewController 之前在我的自定义UIStoryboardSegues 中调用它:
[UIViewController attemptRotationToDeviceOrientation];
或
NSNumber *numPortrait = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:numPortrait forKey:@"orientation"];
但是没有一个工作。当然最后一个例子不应该是一个选项,因为如果苹果会改变他们的任何 api 这可能会导致你的应用程序内部出现问题。
我也尝试使用AppDelegate 方法,并在寻找实际visibleViewController 的正确UIInterfaceOrientation 后始终确定此方法内的方向,但有时在从一个方向切换到另一个方向时会发生崩溃。所以我仍然想知道为什么它变得如此复杂,而且似乎也没有任何文档可以正确解释它。
甚至关注this part didn't help我。