【问题标题】:Black Translucent Navigation Bar Flashes Opaque on First Push黑色半透明导航栏在第一次按下时闪烁不透明
【发布时间】:2020-10-15 10:50:58
【问题描述】:

我有一个导航控制器,我在其中设置了 navigationBar.barStyle = UIBarStyleBlack 和 navigationBar.translucent = YES(根据 Apple 的建议,因为他们弃用了 UIBarStyleBlackTranslucent)。在模拟指标的两个 nib 文件(这不是使用情节提要)中,我将 Top Bar 设置为 Black Navigation Bar。

SettingsViewController *controller = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];

controller.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closeSettings)];

controller.navigationController.navigationBar.barStyle = UIBarStyleBlack;
controller.navigationController.navigationBar.translucent = YES;

[self presentViewController:navController animated:YES completion:nil];

当我展示导航控制器时,它会以黑色半透明条正确打开,但是当我推送到下一个表格视图时,导航栏会在大约 200 毫秒的过程中迅速淡化为不透明,然后又恢复为半透明。它几乎闪烁不透明,然后恢复为半透明。

然后当我推到下一个表格视图或返回(通过按下导航栏左上角的按钮,或通过弹出视图)时,它不会闪烁。在整个导航控制器被解除之前,它始终保持半透明。

我认为这可能是因为笔尖设置不透明条的方式,但我尝试了每种类型的选项(半透明、常规蓝色、无条),但它仍然可以。

此外,它在我的应用程序中的两个完全独立的导航控制器上执行此操作。抱歉,如果我做的事情明显错了,但我尝试了这么多选项组合,只是不知所措。

谢谢!

【问题讨论】:

    标签: ios


    【解决方案1】:

    我认为你根本不应该使用 BarStyle,而只是:

    [controller.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    [controller.navigationController.navigationBar setTranslucent:YES];
    

    我还会尝试对样式更改进行动画处理,以使用呈现的视图控制器 viewDidAppear 方法中的样式更改来获得更平滑的效果(请考虑从代码中的黑色不透明条开始):

    ...
    // Keep that where you want
    
    SettingsViewController *controller = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
    
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
    
    controller.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closeSettings)];
    
    // remove the two lines
    
    [self presentViewController:navController animated:YES completion:nil];
    
    }
    

    然后在 SettingViewController 代码中放:

    - (void) viewDidAppear:(BOOL)animated {
        [UIView beginAnimations:nil context:nil]; 
        [UIView setAnimationDuration:duration];
    
        [controller.navigationController.navigationBar setTintColor:[UIColor blackColor]];
        [controller.navigationController.navigationBar setTranslucent:YES];
    
        [UIView commitAnimations];
    }
    

    【讨论】:

      【解决方案2】:

      为您的UIViewController 实例属性edgesForExtendedLayout 设置适当的值,并将backgroundColor 设置为您的navigationController,例如:

      self.edgesForExtendedLayout = UIRectEdgeBottom;
      self.navigationController.view.backgroundColor = [UIColor whiteColor];
      

      希望对你有帮助。

      SWIFT 5 版本

          self.edgesForExtendedLayout = UIRectEdge.bottom
          self.view.backgroundColor = .white
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多