Moham Qmaizer
Moham Qmaizer

Reputation: 129

change uinavigation bar item position

How can i change UINavigation barItem position from leftBarButtonItem to rightBarButtonItem with back function

enter image description here

Upvotes: 1

Views: 821

Answers (2)

Nirav D
Nirav D

Reputation: 72460

For that you need to create a custom BarButtonItem and set it to the right side, also you need to hide the default BackBarButton, so try some thing like this. Add below code inside viewDidLoad.

self.navigationItem.hidesBackButton = true
let btnForward = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
btnForward.setImage(UIImage(named: "forward_Arrow"), forState: .Normal)
btnForward.addTarget(self, action: #selector(self.buttonAction(_:)), forControlEvents: .TouchUpInside)
let backItem = UIBarButtonItem(customView: btnForward)     
self.navigationItem.rightBarButtonItem = backItem

After that add this action method inside your ViewController

func buttonAction(sender: UIButton) {
    self.navigationController?.popViewControllerAnimated(true)
}

Upvotes: 2

Himanshu
Himanshu

Reputation: 2842

In ViewDidLoad write,

    let rightButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target: self, action: "buttonMethod")
rightButton.setImage(UIImage(named: "arrow.png"), forState: UIControlState.Normal)
            navigationItem.rightBarButtonItem = rightButton

And it's function for pop up

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

Upvotes: 1

Related Questions