更改pushViewController上的navigationBar:
在我的基于 tabBar 的应用程序中,我对 UINavigationBar 进行了子类化。假设我有三个:BlueNavBar、BlackNavBar 和 RedNavBar。它看起来像这样:
//BlueNavBar.m
- (void)drawRect:(CGRect)rect {
self.tintColor = [UIColor colorWithRed:65.0f/255.0f green:(156.0f/255.0f) blue:(215.0f/255.0f) alpha:1.0];
UIImage *image = [[UIImage imageNamed:@"blueNavBar.png"]retain];
[image drawInRect:rect];
[image release];
}
我已经使用 Interface Builder 为每个选项卡分配了子类导航栏。效果很好,没有任何问题。
然而,在某些 viewController 中,我想在“pushViewController”期间更改导航栏。假设我想将当前导航栏(例如 BlueNavBar)更改为 RedNavBar。在没有 Interface Builder 的情况下,如何以编程方式执行此操作?
In my tabBar based app I have subclassed the UINavigationBar. Let's say I have three of them: BlueNavBar, BlackNavBar and RedNavBar. It looks something like this:
//BlueNavBar.m
- (void)drawRect:(CGRect)rect {
self.tintColor = [UIColor colorWithRed:65.0f/255.0f green:(156.0f/255.0f) blue:(215.0f/255.0f) alpha:1.0];
UIImage *image = [[UIImage imageNamed:@"blueNavBar.png"]retain];
[image drawInRect:rect];
[image release];
}
I've assigned the subclassed navigationbar for each tab with Interface Builder. That is working great, no problems there.
In some viewControllers however i want to change the navigationBar during "pushViewController". Let's say I want to change the current navigationbar (which is for e.g. BlueNavBar) to the RedNavBar. How can I do this programmatically, without Interface Builder?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您如何设计视图控制器类本身。设计您需要的一种方法是在创建视图控制器时将其推入堆栈之前设置导航栏类型(即颜色)。类似这样:
navigationBarStyle
的 setter 方法将为视图控制器(重新)创建一个适当颜色的导航栏。It depends on how you've designed the view controller classes themselves. One way to design what you need would be to set the navigation bar type (i.e. colour) when you create the view controller, before you push it on the stack. Something like:
The setter method for
navigationBarStyle
would then (re)create an appropriately-coloured navigation bar for the view controller.