UINavigationController TitleView - 需要澄清

时间:2013-07-12 08:26:31

标签: objective-c uinavigationcontroller titleview

好的,有一点我真的不明白。

我有一个导航控制器(在AppDelegate.m中创建)作为标签栏控制器中的第一项(在AppDelegate.h中创建):

self.tabBarController.viewControllers = @[tabOneNavigationController, viewController2, viewController3, viewController4, viewController5];

在另一个类中,我通过以下方式访问此tabOneNavigationController:

AppDelegate *apd = (AppDelegate *) [[UIApplication sharedApplication] delegate];
UINavigationController *navtab1 = [apd.tabBarController.viewControllers objectAtIndex:0];

如果我想更改navtab1导航栏的背景,我写道:

[navtab1.navigationBar setBackgroundImage:navigationBackgroundImage forBarMetrics:UIBarMetricsDefault]; 

但是为了更改titleView,我只看到了使用的示例:

self.navigationItem.titleView = ...

但是如何更改navtab1的titleView?

示例:我有一个自定义的TableViewCell,其中包含一个按钮,当单击该按钮时,它应该更改navtab1的titeView(在这种情况下,self显然没有navigationItem属性。

1 个答案:

答案 0 :(得分:0)

以下是使用UINavigationController的委托方法,此代码可能对您的情况有所帮助:

#pragma mark -
#pragma mark - UINavigationController Delegate Methods

- (void)navigationController:(UINavigationController *)navigationController  willShowViewController:(UIViewController *)viewController  animated:(BOOL)animated
{
    UINavigationBar *morenavbar = navigationController.navigationBar;
    UINavigationItem *morenavitem = morenavbar.topItem;
    morenavitem.titleView = myCustomeView; // Add Here your custom UIView.
    morenavitem.rightBarButtonItem.tintColor = [UIColor blackColor];

}

在上面的方法中,您还可以放置NavigationBar的图像/颜色。