【问题标题】:iOS 5: Can you override UIAppearance customisations in specific classes?iOS 5:你能覆盖特定类中的 UIAppearance 自定义吗?
【发布时间】:2012-01-17 14:11:35
【问题描述】:

我正在构建一个包含许多视图控制器的应用程序:我有一个 UITabBarController,其中包含 4 个 UINavigationController。我希望所有导航栏都是我的自定义颜色,比如蓝色,我通过在我的应用程序委托中执行此操作来实现:

[[UINavigationBar appearance] setTintColor:[UIColor blueColor]];

但是我也有一个特殊的视图控制器,它有一个地图,对于这个视图控制器,我想覆盖使用UIAppearance 设置的蓝色导航栏,使其成为黑色不透明样式。我已经尝试通过在viewDidLoad 内部调用它:

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

但是什么也没发生。可以这样做还是我必须放弃 UIAppearance 并为每个视图控制器手动设置 navigationBar 上的属性?

【问题讨论】:

    标签: objective-c ios xcode4 ios5


    【解决方案1】:

    你这样做的方式应该有效,但事实并非如此。这确实有效:

    斯威夫特 4

    UINavigationBar.appearance(whenContainedInInstancesOf: [YourOtherVC.self]).tintColor = .black
    

    目标-C

    [[UINavigationBar appearanceWhenContainedIn:[YourOtherVC class], nil] setTintColor:[UIColor blackColor]];
    

    【讨论】:

      【解决方案2】:

      将您的更改移动到 viewWillAppear: 而不是 viewDidLoad: 它应该可以工作。

      【讨论】:

        【解决方案3】:

        你会这样做:

        id specialNavBarAppearance = [UINavigationBar appearanceWhenContainedIn:[SpecialViewController class], nil];
        
        [specialNavBarAppearance setBarStyle:UIBarStyleBlack];
        [specialNavBarAppearance setTranslucent:YES];
        

        【讨论】:

        • 我的SpecialViewController 的视图只是被推送到导航控制器堆栈上。我已经在 Storyboard 中创建了视图层次结构,所以我的视图控制器都没有指向相关导航控制器的指针。我理解它的方式,有问题的 UINavigationBar 需要包含在我与之交谈的外观代理的容器类中。这是否意味着我必须放弃情节提要?
        • 在外观代理上调用 setTranslucent 实际上不起作用。它说Illegal property type, c in invocation selector, _UIAppearance_setTranslucent:'
        猜你喜欢
        • 2019-05-02
        • 2012-06-06
        • 1970-01-01
        • 2012-01-09
        • 2013-07-13
        • 2016-04-04
        • 2016-10-22
        • 2014-10-11
        • 2016-11-30
        相关资源
        最近更新 更多