UITabBar更改所有旋转视图

问题描述

| 我有一个带有多个视图的标签栏控制器。每个视图都有一个工具栏。我添加了将背景图像应用于工具栏的代码。 我还在每个视图上的viewdidLoad中添加了代码,以在设备旋转时触发,因此我可以为横向模式应用不同的背景图片:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(didRotate:)
name:@\"UIDeviceOrientationDidChangeNotification\" object:nil];
如果我运行该应用程序并旋转,则第一个视图有效,但随后转到其他选项卡将导致didRotate方法不触发,因为设备已经旋转。 设备旋转时如何使所有视图更新?     

解决方法

您不一定需要通知。您可以使用两种方法来完成此操作
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation 
                                duration:(NSTimeInterval)duration
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
例如,要使tabBar在横向模式下隐藏而不在纵向模式下隐藏,请使用以下命令:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    return YES;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation 
                                duration:(NSTimeInterval)duration
{
        if (toOrientation == UIInterfaceOrientationLandscapeLeft ||
            toOrientation == UIInterfaceOrientationLandscapeRight) {
            [[self view] endEditing:YES];
            [[self view] addSubview:graphView];
            //[[UIApplication sharedApplication] setStatusBarHidden:TRUE withAnimation:FALSE];
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
            [self.navigationController setNavigationBarHidden:TRUE animated:TRUE]; 
        }       

}

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    UIInterfaceOrientation toOrientation = self.interfaceOrientation;

    if ( self.tabBarController.view.subviews.count >= 2 )
    {
        UIView *transView = [self.tabBarController.view.subviews objectAtIndex:0];
        UIView *tabBar = [self.tabBarController.view.subviews objectAtIndex:1];

        if(toOrientation == UIInterfaceOrientationLandscapeLeft ||
           toOrientation == UIInterfaceOrientationLandscapeRight) {                                     
            transView.frame = CGRectMake(0,480,320 );
            tabBar.hidden = TRUE;
        }
        else
        {                               
            transView.frame = CGRectMake(0,320,480);         
            tabBar.hidden = FALSE;
        }
    }
}
    ,您应该做的是在
viewWillAppear:
上选中
interfaceOrientation
,并在需要时重新布局UI。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...