如何在每页的基础上更改导航栏的背景图像?
我一直在寻找一种方法来更改 NavigationBar
的背景图像并在用户导航应用程序时控制 NavigationBar
的外观。
据我所知,更改背景图像的可接受方法是:
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"navbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
但是,这会改变整个应用程序中 NavigationBar
的外观。当用户从一个视图导航到下一个视图时,如何更改 NavBar
的背景图像?
预先感谢您的帮助!
I've been looking around for a way to change the background image of my NavigationBar
and control the appearance of my NavigationBar
as the user navigates the app.
I understand that the accepted approach for changing the background image is :
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"navbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
However, that changes the appearance of the NavigationBar
throughout the whole app. How can I change the background image of the NavBar
as the user navigates from one view to the next?
Thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要在当前页面或当前要使用的适当图像的某个位置设置一些状态,可能在每个
viewWillAppear:
方法中。然后修改上面的drawRect:
函数以引用该状态。要使栏重新绘制,请在更新状态时调用
[myNavigationBar setNeedsDisplay]
。这将导致调用drawRect
。You need to set some state somewhere about the current page or currently appropriate image to use, probably in each of your
viewWillAppear:
methods. Then modify yourdrawRect:
function above to reference that state.To cause the bar to be redrawn, call
[myNavigationBar setNeedsDisplay]
when you update the state. This will causedrawRect
to be invoked.我真的建议阅读 Sebastian Celis 的教程,它确实对我有帮助 - http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/
I'd really recommend reading a tutorial from Sebastian Celis, it really helped me - http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/
是否可以在导航栏上使用图像视图(添加)?
isn't it possible to use an image view (add) on the navigation bar?
我相信这是 iOS5 的第一个实际答案,主要问题是完成后背景图像的“删除”。好吧,只需保留现有图像并在完成后将其放回去即可。
I believe this to be the first actual answer to this for iOS5, the main problem being the "removal" of the background image once you are done. Well, just retain the existing image and put it back when you are done.
您可以在应用程序中的任何位置使用此代码(调用 viewWillAppeare 中的方法:),通过调用该方法来更改导航栏图像。如果您在 didFinishLanch 中调用该方法,则意味着导航栏图像已设置为整个应用程序。
You can use this code any where in your application(Call the method in viewWillAppeare:) by calling the method to change the navigation bar image. If You call the method in didFinishLanch means the navigation bar image is set to the whole app.