我有一个纵向模式的 UITabBarViewController
,我在景观中推动 modalViewController
. 当我按下模态时,旋转会变为横向,但是当我将其关闭时,方向仍保留在横向中而不是恢复为纵向 .
码:
a CustomUINavigationController
用于横向模式:
- (BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
uitabbarcontroller
- (BOOL)shouldAutorotate
{
if ([self inModal]) {
UINavigationController *nav = (UINavigationController *)self.modalViewController;
return [[nav.viewControllers lastObject] shouldAutorotate];
} else {
return NO;
}
}
-(NSUInteger)supportedInterfaceOrientations
{
if ([self inModal]) {
UINavigationController *nav = (UINavigationController *)self.modalViewController;
return [[nav.viewControllers lastObject] supportedInterfaceOrientations];
} else {
return UIInterfaceOrientationPortrait;
}
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
if ([self inModal]) {
UINavigationController *nav = (UINavigationController *)self.modalViewController;
return [[nav.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
} else {
return UIInterfaceOrientationPortrait;
}
}
- (BOOL)inModal
{
if (self.modalViewController && [self.modalViewController isKindOfClass:[UINavigationController class]]) {
return YES;
} else {
return NO;
}
}
如果我将shouldRotate设置为YES,则会出现错误:*由于未捕获的异常'UIApplicationInvalidInterfaceOrientation'而终止应用程序,原因:'支持的方向与应用程序没有共同的方向,并且shouldAutorotate返回YES . 即使我在UISupportedDeviceOrientations中设置了两个方向 .
1 回答
我认为你的问题是由这一行造成的(你的代码中有两次):
我正在尝试做s.th.类似于你想要达到的目的,并在这里问我的问题:iOS 6 auto rotation issue - supportedInterfaceOrientations return value not respected
您的代码的问题在于这两种方法不返回接口方向,而是返回一个掩码,该掩码在其位中编码允许或首选方向的组合 .
试试这个:
据我所知,
UIInterfaceOrientationPortrait
被映射为0,因此被解释为一组空接口方向(无) .