使用标签栏控制器时的方向问题
Posted
技术标签:
【中文标题】使用标签栏控制器时的方向问题【英文标题】:orientation issues while using tab bar controller 【发布时间】:2013-07-16 13:29:03 【问题描述】:我在有 4 个标签的 iPhone 应用中使用标签栏控制器。我已经为这两个方向编写了代码。
问题是,当我将标签页从纵向视图切换到横向视图时,我遇到了麻烦。例如。如果我处于 tab1 横向模式,现在我切换标签并移动到 tab2 横向模式,但我可以看到该特定视图的纵向设置,而不是横向视图。
到目前为止,包含标签栏控制器的启动视图控制器上的代码如下:
-(void)viewWillAppear:(BOOL)animated
[self.navigationController setNavigationBarHidden:YES animated:animated];
if (self.interfaceOrientation==UIInterfaceOrientationLandscapeLeft)
[self rotatingLandscape];
else if (self.interfaceOrientation==UIInterfaceOrientationLandscapeRight)
[self rotatingLandscape];
if (self.interfaceOrientation==UIInterfaceOrientationPortrait)
[self RotatingPotrait];
else if (self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
[self RotatingPotrait];
[super viewWillAppear:animated];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
if (interfaceOrientation==UIInterfaceOrientationPortrait)
rotate=YES;
[self RotatingPotrait];
if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
rotate=YES;
[self RotatingPotrait];
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
rotate=NO;
[self rotatingLandscape];
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
rotate=NO;
[self rotatingLandscape];
return YES;
-(void)rotatingLandscape
int selIndex = [self.tabBarController selectedIndex];
if (selIndex == 0)
[view1p rotatingLandscape];
else if (selIndex == 1)
[view2 rotatingLandscape];
else if (selIndex == 2)
[view3 rotatingLandscape];
else if (selIndex == 3)
[view4 rotatingLandscape];
self.tabBarController.view.frame=CGRectMake(0, 0, 480, 300);
-(void)RotatingPotrait
int selIndex = [self.tabBarController selectedIndex];
if (selIndex == 0)
[view1p RotatingPotrait];
else if (selIndex == 1)
[view2 RotatingPotrait];
else if (selIndex == 2)
[view3 RotatingPotrait];
else if (selIndex == 3)
[view4 RotatingPotrait];
self.tabBarController.view.frame=CGRectMake(0, 0, 320, 460);
【问题讨论】:
题外话:你应该使用switch
语句和/或UIInterfaceOrientationIsLandscape()
/UIInterfaceOrientationIsPortrait()
我没明白,请详细说明
@Kevin :- 你能帮我解决这个问题吗?
【参考方案1】:
我已经解决了我的项目支持 ios 6.0+ 的问题 步骤:
1创建UITabBarViewController的子类并添加
- (BOOL)shouldAutorotate
return [self.selectedViewController shouldAutorotate];
- (NSUInteger)supportedInterfaceOrientations
return [self.selectedViewController supportedInterfaceOrientations];
2 子类 UINavigationController 添加
- (BOOL)shouldAutorotate
return [self.topViewController shouldAutorotate];
- (NSUInteger)supportedInterfaceOrientations
return [self.topViewController supportedInterfaceOrientations];
3 到 AppDelegate 添加
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController)
UIViewController *presentedViewController = [[[[(UINavigationController *)self.window.rootViewController viewControllers] objectAtIndex:self.tabBarController.selectedIndex] viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
return orientations;
向每个 UIViewController 添加 4 个 self.tabBarCotroller.delegate = self;
和委托方法
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
NSLog(@"Tab index = %u ", indexOfTab);
UIViewController *mVC = [[UIViewController alloc] init];
mVC.view.backgroundColor = [UIColor redColor];
mVC.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:mVC animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
为每个 UIViewController 添加所需的方向
- (BOOL)shouldAutorotate
if (self.interfaceOrientation==UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation==UIInterfaceOrientationLandscapeRight)
return NO;
else
return YES;
- (NSUInteger)supportedInterfaceOrientations
return UIInterfaceOrientationMaskLandscape; //UIInterfaceOrientationMaskPortraite
对我来说这是工作
我创建了example
【讨论】:
以上是关于使用标签栏控制器时的方向问题的主要内容,如果未能解决你的问题,请参考以下文章