遍历导航层次结构时更改UINavigationBar项

时间:2013-05-06 14:49:39

标签: ios uinavigationcontroller uikit

我正在尝试使用以下代码自定义UINavigationBar中的项目:

    CGSize size = [UIImage imageNamed:kGlobalNavigationBarButtonBackOrange].size;
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    [backButton setImage:[UIImage imageNamed:kGlobalNavigationBarButtonBackOrange] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:kGlobalNavigationBarButtonBackPressedOrange] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];

    UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Detail"];
    [item setLeftBarButtonItem:barButton];
    [item setTitleView:self.navigationItem.titleView];

    [self.navigationBar pushNavigationItem:item animated:YES];
    [self.navigationController pushViewController:vc animated:YES]; 

然而,当我运行它时,导航栏看起来没有任何不同。

1 个答案:

答案 0 :(得分:1)

您尝试做的事情应该在vc视图控制器中完成。重点是,当一个新的视图控制器被推入堆栈时,会询问它的导航项目,并用于更新导航栏。

在视图控制器的- viewDidLoad中,创建条形按钮并将其设置为self. navigationItem. leftBarButtonItem

您无需创建新导航项或将任何导航项推送到导航栏。让视图控制器框架为您完成此任务。