iOS 动态更改 UINavigationBar 的背景与图像

发布于 2024-11-28 12:54:01 字数 539 浏览 0 评论 0原文

我已阅读​​此并更改 UINavigationBar 的背景:

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"top-with-logo.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

你看,我添加了一个名为 top- 的图像with-logo.png,我想通过添加名为 top-without-logo.png 的图像来更改背景视图。但上面的代码将永远改变背景,我不能再改变了。你知道怎么做吗?

非常感谢。

I have read this and change UINavigationBar's background:

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"top-with-logo.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

you see, I add a image named top-with-logo.png, and I want to change the background view by adding an image named top-without-logo.png. But the code above will change the background forever and i cannont change any more. Do you know how?

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┈┾☆殇 2024-12-05 12:54:01

我不确定我明白你的意思,但你可以在你的应用程序的委托上放置一个布尔 ivar。然后在函数中,如果是 true/false,则加载 top-with-logo.png;如果是其他值,则加载 top-without-logo.png。然后,当您想要更改徽标时,只需更改布尔值并在导航栏上调用 setNeedsDisplay 即可。

如果您使用xib文件,您还可以对UINavigationBar进行子类化,而不是使用类别,您可以重写drawRect函数并将变量作为实例变量放置为子类,我相信它更干净,但您只能在Interface中设置构建器(否则无法告诉 UINavigationController 对 navigationBar 属性使用不同的类)

I'm not sure I understand what you mean, but you can place a Boolean ivar on your app's delegate. then in the function you load top-with-logo.png if it's true/false and load top-without-logo.png if it's the other value. then, when you want to change the logo, just change the Boolean's value and call setNeedsDisplay on the navigationBar.

if you use xib files, you can also subclass UINavigationBar, and instead of using a catagory you override the drawRect function and place the variable as an instance variable as a subclass, I believe it is cleaner, but you can only set that up in Interface builder ( otherwise there's no way to tell a UINavigationController to use a different class for the navigationBar property )

软甜啾 2024-12-05 12:54:01

你可以使用,

UINavigationBar *navigationBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed: @"NavBar-Wood.png"];
[navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];

You can use,

UINavigationBar *navigationBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed: @"NavBar-Wood.png"];
[navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文