user2695433 user2695433 - 1 year ago 187
Swift Question

Back Button Left Alignment IOS 9

I m trying to left align back button ie remove the space on the left of the back arrow . Using a custom back button .

let backButton = UIBarButtonItem(image: UIImage(named: "arrow03"), style: .Plain, target: self, action: "back")

self.navigationController?.navigationBar.tintColor = UIColor.clearColor()
self.navigationItem.backBarButtonItem = backButton

Tried to use negative width for the button as suggested in the below SO link but it didnt work.
How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar [iOS 7]
Please help.

Answer Source
    let button: UIButton = UIButton (type: UIButtonType.Custom)
    button.setImage(UIImage(named: "imageName"), forState: UIControlState.Normal)
    button.addTarget(self, action: "backButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
    button.frame = CGRectMake(0, 0, 30, 30)
    let barButton = UIBarButtonItem(customView: button)

    self.navigationItem.leftBarButtonItem = barButton

Make sure your image has to be plain ( transparent ) background.

func backButtonPressed(btn : UIButton) {