更改pushViewController上的navigationBar:

发布于 2024-09-16 23:22:43 字数 675 浏览 12 评论 0原文

在我的基于 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 技术交流群。

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

发布评论

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

评论(1

漫雪独思 2024-09-23 23:22:43

这取决于您如何设计视图控制器类本身。设计您需要的一种方法是在创建视图控制器时将其推入堆栈之前设置导航栏类型(即颜色)。类似这样:

SomeViewController* someViewController = [[SomeViewController alloc] initWithNibName:@"SomeView" bundle:nil];
someViewController.navigationBarStyle = NBStyleRed; // NBStyleRed defined as an enum somewhere
[self.navigationController pushViewController:someViewController animated:YES];
[someViewController release];

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:

SomeViewController* someViewController = [[SomeViewController alloc] initWithNibName:@"SomeView" bundle:nil];
someViewController.navigationBarStyle = NBStyleRed; // NBStyleRed defined as an enum somewhere
[self.navigationController pushViewController:someViewController animated:YES];
[someViewController release];

The setter method for navigationBarStyle would then (re)create an appropriately-coloured navigation bar for the view controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文