我在我的iPhone应用程序中使用标签栏控制器,有4个标签。我为两个方向编写了代码。
问题是当我将标签从纵向切换到横向视图时我遇到了麻烦。例如。如果我处于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);
}
答案 0 :(得分: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];
}
5为每个UIViewController添加你需要的方向
- (BOOL)shouldAutorotate
{
if (self.interfaceOrientation==UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation==UIInterfaceOrientationLandscapeRight)
{
return NO;
} else {
return YES;
}
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape; //UIInterfaceOrientationMaskPortraite
}
对我来说这是工作
我创建了example