如何在每页的基础上更改NavigationBar的背景图像? [英] How can I change the background image for my NavigationBar on a per page basis?

查看:138
本文介绍了如何在每页的基础上更改NavigationBar的背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方法来改变我的 NavigationBar 的背景图像,并控制我的 NavigationBar 当用户导航应用时。

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

然而,这改变了外观整个应用程序中的 NavigationBar 当用户从一个视图导航到下一个视图时,如何更改 NavBar 的背景图像?

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?

谢谢提前帮助你!

推荐答案

您需要在当前页面或当前适当的图像上设置一些状态,可能是在每个 viewWillAppear:方法中。然后修改上面的 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 your drawRect: function above to reference that state.

要使条形图重绘,请调用<更新状态时code> [myNavigationBar setNeedsDisplay] 。这将导致调用 drawRect

To cause the bar to be redrawn, call [myNavigationBar setNeedsDisplay] when you update the state. This will cause drawRect to be invoked.

这篇关于如何在每页的基础上更改NavigationBar的背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆