【问题标题】:How to ovverride UINavigationBar appearance if already customized如果已经自定义,如何覆盖 UINavigationBar 外观
【发布时间】:2012-01-17 19:13:54
【问题描述】:

在我的应用程序的应用程序委托中,我调用以下方法:

- (void)customizeAppearance 
{
    UIImage *gradientPortrait = [[UIImage imageNamed:@"gradient_portrait"] 
        resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

    UIImage *gradientLandscape = [[UIImage imageNamed:@"gradient_landscape"] 
        resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

    [[UINavigationBar appearance] setBackgroundImage:gradientPortrait 
        forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:gradientLandscape 
        forBarMetrics:UIBarMetricsLandscapePhone];
}

此代码允许自定义应用程序中的所有导航栏。因为我使用的图像是绿色的,所以每个条都变成绿色。

现在我的目标是为特定导航栏覆盖上述配置。 特别是,在应用程序生命周期中,我使用UIModalPresentationFormSheet 表示样式打开了一个模态控制器。此控制器显示在 UINavigationController 中。由于我还需要显示与 UINavigationController 相关的导航栏,我想知道如何在不更改我在应用程序委托中设置的全局配置的情况下自定义该栏。

我尝试将导航栏的tintColor 属性(以模态方式呈现)设置为[UIColor blackColor],并将barStyle 设置为UIBarStyleBlack,但它们不起作用。只有 barbutton 项目受到影响。

提前谢谢你。

P.S.我使用的是 iOS 5

【问题讨论】:

标签: ios ios5 uinavigationbar styling appearance


【解决方案1】:

首先,如果您不介意拉伸图像,则不需要使用 resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) 方法。

UIImage *gradientPortrait = [UIImage imageNamed:@"gradient_portrait"];
UIImage *gradientLandscape = [UIImage imageNamed:@"gradient_landscape"];

UIImage *someOtherImage = [UIImage imageNamed:@"someOtherImageName"];

然后,实现你想要的:

  1. 确保将 ViewController 子类化,自定义导航栏会出现
  2. 使用以下命令将默认图像添加到所有导航栏

    [[UINavigationBar appearance] setBackgroundImage:gradientPortrait forBarMetrics:UIBarMetricsDefault];
    
  3. 使用以下方法将特定图像添加到导航栏,该导航栏将出现在子类 ViewControllers 上方

    [[UINavigationBar appearanceWhenContainedIn:[YOURSUBCLASS class], nil] setBackgroundImage:someOtherImage forBarMetrics:UIBarMetricsDefault];
    

(您可以从应用委托中的某处调用这些方法)

顺便说一句,如果您只想更改色调颜色,您可以使用 tintColor 属性而不是所有图像。

更多信息请查看外观部分:UINavigationBar Class Reference

【讨论】:

  • 第三步我已经试过了,但是还是不行。实时定制每个导航栏(当它被创建时)它可以工作。谢谢。
  • 会出现什么自定义导航栏?那是代表吗
【解决方案2】:

使用您的自定义类子类 UINavigationBar 并在其上设置不同的外观。然后在你有不同外观的特定地方使用你的自定义类。

【讨论】:

  • 我尝试将UINavigationBarUINavigationController 子类化,并直接设置值或使用带有whenContainedInInstancesOf 的外观代理,但都不起作用
猜你喜欢
  • 2017-07-05
  • 1970-01-01
  • 2015-04-07
  • 1970-01-01
  • 2017-10-29
  • 1970-01-01
  • 1970-01-01
  • 2012-02-20
  • 1970-01-01
相关资源
最近更新 更多