0

I'm trying to replace the UINavigationController BackBarButtonItem with a custom image, and I would like there to be no back icon like it's currently doing here:

enter image description here

I'm taking this picture from another stack post, but something similar is happening with mine

The solution to this was listed here: Remove back arrow in iOS7

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back-btn"]
                                                                         style:UIBarButtonItemStylePlain
                                                                        target:nil
                                                                        action:nil];

if ([UINavigationBar instancesRespondToSelector:@selector(setBackIndicatorImage:)]) {
    [[UINavigationBar appearance] setBackIndicatorImage:[[UIImage alloc] init]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[[UIImage alloc] init]];
}

The problem is I'm having trouble converting this code to Swift. If anybody could help me that would be greatly appreciated. Thanks in advance!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Thomas
  • 2,356
  • 7
  • 23
  • 59
  • You can get http://stackoverflow.com/questions/28421769/how-to-replace-customize-back-button-image-in-storyboard-navigationcontroller or http://stackoverflow.com/questions/26936296/custom-back-button-with-image – Saavaj Dec 29 '16 at 04:40
  • Thanks @Saavaj. According to the second link, they're doing it globally. Do I have to do it globally? – Thomas Dec 29 '16 at 04:44

4 Answers4

2

For getting swipe back feature : First set delegate in viewDidLoad:

self.navigationController!.interactivePopGestureRecognizer.delegate = self

And then disable gesture when pushing:

override func pushViewController(_ viewController: UIViewController, animated: Bool) {
    super.pushViewController(viewController, animated: animated)
    self.interactivePopGestureRecognizer.isEnabled = false
}

And enable in viewDidDisappear:

self.navigationController!.interactivePopGestureRecognizer.isEnabled = true
Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45
Rupali Patil
  • 199
  • 4
0

Use below code:

//Hide Default Back Button First
    self.navigationItem.setHidesBackButton(true, animated:true);

//Your code to show back button
    let backButton = UIButton()
    backButton.setImage(UIImage(named: "imagename"), forState: .Normal)
    backButton.frame = CGRectMake(0, 0, 30, 30)
    backButton.addTarget(self, action: Selector("action"), forControlEvents: .TouchUpInside)

    let rightBarButton = UIBarButtonItem()
    rightBarButton.customView = backButton
   self.navigationItem.leftBarButtonItem = rightBarButton

//Back Button Action

func action()
    {
        self.navigationController?.popViewControllerAnimated(true)
    }

Or you can refer one of the answer

Community
  • 1
  • 1
Dheeraj D
  • 4,386
  • 4
  • 20
  • 34
0

Use following code in swift 3 :

class BaseViewController: UIViewController  {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setBackButton()
    }

    func setBackButton() {
        let backImage = UIImage(named:"back_icon") as UIImage!
        let backButton = UIBarButtonItem(image: backImage, style: UIBarButtonItemStyle.plain, target: self, action: #selector(BaseViewController.viewWillDisappearC) )

        self.navigationItem.leftBarButtonItem = backButton
    }

    func viewWillDisappearC() {
        _ = self.navigationController?.popViewController(animated: true)
    }
}
anacron
  • 6,443
  • 2
  • 26
  • 31
Rupali Patil
  • 199
  • 4
0

Use following code in swift 2.2 (xocde 7.3) :

class BaseViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    self.setBackButton()
}

func setBackButton() {
    let backImage = UIImage(named:"back_icon") as UIImage!
    let backButton = UIBarButtonItem(image: backImage, style:UIBarButtonItemStyle.Plain, target: self, action: #selector(BaseViewController.viewWillDisappearC) )
    self.navigationItem.leftBarButtonItem = backButton
}

func viewWillDisappearC() {
    self.navigationController?.popViewControllerAnimated(true)
}

}

Rupali Patil
  • 199
  • 4