我有viewcontroller X和viewcontroller Y。
viewController X呈现Y,Viewcontroller X应该只以纵向模式显示,但是Y可以旋转到它想要的任何模式。
当我在横向模式下解除viewController Y时,我收到以下错误。 在线:
[self dismissViewControllerAnimated:YES completion:nil];
错误: 由于未捕获的异常'UIApplicationInvalidInterfaceOrientation'而终止应用程序,原因:'preferredInterfaceOrientationForPresentation必须返回支持的接口方向!'
在X Controller中,我有:
-(BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
在Y控制器中我有:
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
ios 6正常运行,但现在在xcode / ios7中引发了问题。现在,即使我正在解雇Y并在横向模式下回到X,我还是设置为更喜欢肖像,所以它不应该只是强制纵向模式而不管我是否有横向设备?
答案 0 :(得分:1)
在关闭子viewController之前添加它:
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"取向"];
假设您的父母是肖像
答案 1 :(得分:0)
在YES
方法中返回shouldAutorotate
。也许你一直在错误的视图控制器中这样做。
答案 2 :(得分:0)
请确保覆盖preferredInterfaceOrientationForPresentation: UIInterfaceOrientation
,因为它会基于关闭的视图控制器的旋转而继承一个值。如果该值与您的视图控制器支持的界面方向不一致,则该应用将崩溃。
/// Returns `.portrait` as the default preferred interface orientation for presentation.
///
/// - Important: It's important to explicitly set this value to a value which is aligned with `supportedInterfaceOrientations`. Not doing so can lead to crashes.
/// In certain scenarios, if not set explicitly this value will be inherited from a dismissing view controller. The application will crash if the dismissing view controller was rotated to an orientation which isn't supported by
/// this view controller. (*** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation 'landscapeRight' must match a supported
/// interface orientation: 'portrait'!')
override public var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}