0

I want to remove the title and use only custom image for back button in Navigation Controller. I came across questions like this and I have tried this:

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

I am putting this in viewDidLoad() of the View Controller which is pushing a UITabbarController. ( I know its not a very good idea to push Tab Bar in a Navigation Controller, but I need to do this).

I am getting the following result: Navigation Bar

But I don't want that default blue back button. I just want my custom close button to appear there. How can I achieve this?

Community
  • 1
  • 1
saurabh
  • 6,687
  • 7
  • 42
  • 63
  • Why you not set image to the left bar button item ? Is it not working? Instate blue back button image ... Or clear color blue back button. – Nazmul Hasan Mar 13 '17 at 17:03
  • Setting left button item isn't working. I don't understand `instate blue back button image` – saurabh Mar 13 '17 at 17:06
  • Check this post may helps http://stackoverflow.com/questions/40504860/how-to-change-back-button-title-on-navigation-controller-in-swift3/40505660#40505660 – Joe Mar 13 '17 at 17:13

2 Answers2

0

Create a bar button

- (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;
}

and set like this

self.navigationItem.leftBarButtonItem = [self backButton];
Navneet Gill
  • 393
  • 1
  • 13
-1
self.navigationController?.navigationBar.backIndicatorImage = backImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage
self.navigationController?.navigationBar.backItem?.title = ""
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: UIControl.State.highlighted)
saurabh
  • 6,687
  • 7
  • 42
  • 63
Rauan
  • 148
  • 1
  • 9
  • What is `setBackIndicatorImage `? – saurabh Mar 13 '17 at 17:04
  • /* The back indicator image is shown beside the back button. The back indicator transition mask image is used as a mask for content during push and pop transitions Note: These properties must both be set if you want to customize the back indicator image. */ – Rauan Mar 13 '17 at 17:13
  • But what is the selector `setBackIndicatorImage ` ? Where is it defined? The compiler is throwing error if I use that. – saurabh Mar 13 '17 at 17:14