我的导航栏出了什么问题?

时间:2016-11-04 09:12:48

标签: ios objective-c uinavigationbar

我想设置当前 navigationItem.backBarItem title 属性,而不是相对于上一个视图的标题。所以我使用这种方法:

self.navigationController.navigationBar.topItem.backBarButtonItem=barButtonItem;

但我发现它在某些情况下并没有像我期望的那样行事。

在视图B中使用TOPITEM

enter image description here

在视图B中使用背景

enter image description here

我发现self.navigationController.navigationBar.items(我认为它是一堆UINavigationItems)似乎不正确。

在视图A中,将self.navigationController.navigationBarHidden=YES;放入viewDidLoadviewWillAppear:也会导致不同的行为。

我的代码出了什么问题?我应该明确访问 self.navigationItem 以强制它初始化(事件我不直接使用它)?

代码:感谢您的耐心

查看A:

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden=YES;
}

查看B:

-(void)viewWillAppear:(BOOL)animated{
    self.navigationItem.title=@"B";
    self.navigationController.navigationBarHidden=NO;
    UIBarButtonItem* buttonItem=[[UIBarButtonItem alloc]
                                 initWithTitle:@"toA"
                                 style:UIBarButtonItemStylePlain
                                 target:self
                                 action:nil];
    self.navigationController.navigationBar.topItem.backBarButtonItem=buttonItem;
}

查看C:

-(void)viewWillAppear:(BOOL)animated{
    self.navigationItem.title=@"C";
    self.navigationController.navigationBarHidden=NO;
    UIBarButtonItem* buttonItem=[[UIBarButtonItem alloc]
                                 initWithTitle:@"toB"
                                 style:UIBarButtonItemStylePlain
                                 target:self
                                 action:nil];
    self.navigationController.navigationBar.topItem.backBarButtonItem=buttonItem;
}

1 个答案:

答案 0 :(得分:4)

如果要设置后退按钮

self.navigationItem.backBarButtonItem=buttonItem;

如果要设置后退按钮的标题,请使用 leftbarbutton viewwillAppear

 [self.navigationItem setHidesBackButton:YES];

 UIBarButtonItem *buttonItem=[[UIBarButtonItem alloc]
                                 initWithTitle:@"toA"
                                 style:UIBarButtonItemStylePlain
                                 target:self
                                 action:@selector(back)];
   self.navigationItem.leftBarButtonItem=buttonItem;

后退行动

-(void)back
{    
[self.navigationController popViewControllerAnimated:YES];

}

或者你可以尝试这些

viewwillappear

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationItem.hidesBackButton=YES;
    UIBarButtonItem *buttonItem=[[UIBarButtonItem alloc]
                                         initWithTitle:@"toA"
                                         style:UIBarButtonItemStylePlain
                                         target:self
                                         action:@selector(back)];
           self.navigationItem.leftBarButtonItem=buttonItem;
    }


-(void)back
        {    
        [self.navigationController popViewControllerAnimated:YES];

        }