iOS 动态更改 UINavigationBar 的背景与图像
我已阅读此并更改 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我明白你的意思,但你可以在你的应用程序的委托上放置一个布尔 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 )
你可以使用,
You can use,