Ios 如果已经定制,如何覆盖UINavigationBar外观

Ios 如果已经定制,如何覆盖UINavigationBar外观,ios,ios5,uinavigationbar,styling,appearance,Ios,Ios5,Uinavigationbar,Styling,Appearance,在我的应用程序中的应用程序委托中,我调用以下方法: - (void)customizeAppearance { UIImage *gradientPortrait = [[UIImage imageNamed:@"gradient_portrait"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; UIImage *gradientLandscape = [[UIImage imageN

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

- (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
presentation样式打开模态控制器。此控制器显示在
UINavigationController
中。由于我还需要显示该
UINavigationController
附带的导航栏,我想知道如何在不更改我在应用程序委托中设置的全局配置的情况下自定义该导航栏

我已尝试将导航栏(以模式显示)的
tintColor
属性设置为
[UIColor blackColor]
,并将
barStyle
设置为
UIBarStyleBlack
,但它们都不起作用。只有按钮项目受影响

先谢谢你


p.S.我使用的是iOS 5

首先,如果您不介意图像拉伸,则无需使用带capinsets:UIEdgeInsetsMake(0,0,0,0)方法调整图像大小

然后,为了实现你想要的:

  • 确保将出现带有自定义导航栏的ViewController子类化
  • 使用以下命令将默认图像添加到所有导航栏中

    [[UINavigationBar appearance] setBackgroundImage:gradientPortrait forBarMetrics:UIBarMetricsDefault];
    
  • 使用以下命令将特定图像添加到导航栏,导航栏将显示在子类ViewController的上方

    [[UINavigationBar appearanceWhenContainedIn:[YOURSUBCLASS class], nil] setBackgroundImage:someOtherImage forBarMetrics:UIBarMetricsDefault];
    
  • (您可以从应用程序代理中的某个位置调用这些方法)

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


    有关更多信息,请查看带有自定义类的子类UINavigationBar的外观部分,并在其上使用不同的外观。然后在特定的地方使用您的自定义类,在那里您可以看到不同的外观。

    检查此SO主题:@KrishnaK谢谢您的回复。我可以在我的定制方法中使用该方法吗?你应该可以在那里使用它。我已经尝试了第三步,但它不起作用。实时自定义每个导航栏(创建时)它可以工作。谢谢。将显示什么自定义导航栏?是被委派者吗?我尝试将
    UINavigationBar
    UINavigationController
    子类化,并在包含Instancesof时直接设置值或使用外观代理
    ,但均无效
    
    [[UINavigationBar appearanceWhenContainedIn:[YOURSUBCLASS class], nil] setBackgroundImage:someOtherImage forBarMetrics:UIBarMetricsDefault];