gpt4 book ai didi

ios - 如果已经自定义,如何覆盖 UINavigationBar 外观

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:31 26 4
gpt4 key购买 nike

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

- (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 呈现样式打开模态 Controller 。此 Controller 显示在 UINavigationController 中。由于我还需要显示与 UINavigationController 相关联的导航栏,我想知道如何自定义该栏,而不更改我在应用程序委托(delegate)中设置的全局配置。

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

提前谢谢你。

附言我使用的是 iOS 5

最佳答案

首先,如果您不介意图像被拉伸(stretch),则无需使用 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. 使用下面的代码将特定图像添加到导航栏中,导航栏将出现在子类 ViewController 上方

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

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

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

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

关于ios - 如果已经自定义,如何覆盖 UINavigationBar 外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8900311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com