仅对navigationItem backBarButton项使用自定义图像

时间:2017-03-13 16:52:07

标签: ios swift uinavigationcontroller uinavigationbar uinavigationitem

我想删除标题,并在导航控制器中仅使用自定义图像作为后退按钮。 我遇到了像this这样的问题,我试过这个:

navigationItem.backBarButtonItem = UIBarButtonItem(image: UIImage(named: Constants.Image.kClose), style: .plain, target: nil, action: nil)

我将它放在View viewDidLoad()中,它正在推动UITabbarController。 (我知道在导航控制器中推动Tab Bar不是一个好主意,但我需要这样做。)

我得到以下结果: Navigation Bar

但我不想要那个默认的蓝色后退按钮。我只想让我的自定义关闭按钮出现在那里。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

创建一个条形按钮

- (UIBarButtonItem *)backButton {
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 4, 34, 34)];
    leftButton.layer.cornerRadius = leftButton.frame.size.width / 2;
    leftButton.layer.masksToBounds = YES;
    [leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    return leftBarButton;
}

并设置如下

self.navigationItem.leftBarButtonItem = [self backButton];

答案 1 :(得分:-1)

 self.navigationItem.backBarButtonItem! = UIBarButtonItem(image: UIImage(named: "back-btn")!, style: .Plain, target: nil, action: nil)
  UINavigationBar.appearance().backIndicatorImage = UIImage()
  UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage()