更新 2
在我的 UITabBarController 子类中,我尝试添加这个:
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
现在,每次我选择一个选项卡项时,设备都会完美地旋转到纵向模式。但是,现在我可以在选择 (a) 时旋转设备,设备将旋转到横向模式。如何停止设备旋转?
我相信我的选项卡 Controller 子类中的这个方法是造成这种情况的原因:
- (BOOL)shouldAutorotate {
if(self.selectedIndex == 0)
{
return NO;
}
return [self.viewControllers.lastObject shouldAutorotate];
}
如果我返回“NO”,当我在 View Controller 中时我不能旋转,但是当我选择它时,它不会自动旋转到纵向。如果我返回"is",我可以在 View Controller 中旋转,但当我选择它时,它会自动旋转为纵向。
我的应用程序中有一个自定义标签栏 Controller ,具有以下层次结构:
UITabBarController
|
UINavigationController
| |
| UIViewController(a)
|
UINavigationController
| |
| UIViewController(b)
|
UINavigationController
|
UIViewController(c)
我希望 View Controller (a) 只能在纵向模式下查看,而 View Controller (b) 和 (c) 可以在所有方向上查看,但要倒置。现在,我可以单独对每个 View Controller 执行此操作,但是当我处于 (b) 横向模式时,我的问题就出现了,并选择 (a) 的选项卡项,然后 a 以横向模式显示,看起来不好的。如何确保选项卡栏(或 View Controller )检查是否可以在当前方向查看要选择的 View Controller ?
如果需要,这是我的 (a) 代码,它自己将其限制为纵向模式:
- (BOOL) shouldAutorotate
{
return NO;
}
- (NSUInteger) supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
更新
我已将此添加到 View Controller (a),但我的 View 中间出现了一个黑色方 block ,并且导航栏中的标题不再居中。
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
最佳答案
自 iOS 7.0 UITabBarController 支持通过 - (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController
进行定向转发。
在您处于此特定选项卡不支持的界面方向时切换到另一个选项卡之前,它会很好用。
不幸的是,在这种情况下,tabbar Controller 不会强制旋转,唯一的方法是使用私有(private) API。
注意:当从仅纵向选项卡切换回支持任何方向的选项卡时,我使用 attemptRotationToDeviceOrientation
将界面旋转到设备方向。
这是我解决问题的方法:
static const NSInteger kPortraitOnlyTabIndex = 1;
@interface TabBarController : UITabBarController<UITabBarControllerDelegate>
@end
@implementation TabBarController
- (id)initWithCoder:(NSCoder *)aDecoder {
if(self = [super initWithCoder:aDecoder]) {
self.delegate = self;
}
return self;
}
- (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController {
if(tabBarController.selectedIndex == kPortraitOnlyTabIndex) {
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[UIViewController attemptRotationToDeviceOrientation];
if([self.viewControllers indexOfObject:viewController] == kPortraitOnlyTabIndex) {
SEL sel = NSSelectorFromString(@"setOrientation:");
if([[UIDevice currentDevice] respondsToSelector:sel]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[[UIDevice currentDevice] performSelector:sel withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)];
#pragma clang diagnostic pop
}
}
}
@end
我将示例应用程序放在 Github 上 https://github.com/pronebird/TabBarOrientationExample
关于ios - 限制选项卡栏 View Controller 中的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27141594/