Swift 3.0 Adding right button to navigation bar

I added a navbar to the top of the view controller. I'm trying to control if a button is visible based on a condition, but I'm having trouble adding a button. Until now I,

var addButton: UIBarButtonItem = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addTapped))

override func viewDidLoad() {
    super.viewDidLoad()

    let boool = true
    if boool {
        self.navigationItem.rightBarButtonItem = self.addButton
    }
    else {
        self.navigationItem.rightBarButtonItem = nil
    }
}

func addTapped(sender: AnyObject) {
    print("hjxdbsdhjbv")
}

      

I believe it is not working correctly because I added a navbar to VC instead of using the nav controller and working with it. I was wondering if there is a way to work with this navigation bar.

+16


source to share


6 answers


You say you added UINavigationBar

to your view controller via storyboard, but looking at the code you provided there is no output connection to your navbar in IB.



To access self.navigationItem

, your view controller must be embedded in UINavigationController

or part of a hierarchy. Unless you have a need for a custom navbar on a separate view controller, I suggest removing that from the Builder interface, and then making sure that any view controller in question is embedded in UINavigationController

or pushed onto the navigation stack from another controller embedded in navigation controller and then you should see yours UIBarButtonItem

.

+4


source


It's simple. Place this line of code in viewDidLoad

:

self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addTapped))

      

Updated for Swift 4 or later:

Custom function:

@objc func action(sender: UIBarButtonItem) {
    // Function body goes here
}

      

(Custom) Right element of the button bar:

self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "some_text", style: .done, target: self, action: #selector(self.action(sender:)))

      



(Custom) Left Button Bar Item:

self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "some_text", style: .done, target: self, action: #selector(self.action(sender:)))

      

You can also add system bar button elements like this: UIBarButtonItem.SystemItem

Defines system images for bar button elements: .add, .done, .cancel, .edit, .save, .compose, .reply, .organize and More.

(System) Right Button Panel:

self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(self.action(sender:)))

      

(System) Left button bar item:

self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(self.action(sender:)))

      

+38


source


let rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "EditImage"), style: .done, target: self, action: #selector(ViewController.call_Method))

self.navigationItem.rightBarButtonItem = rightBarButtonItem

      

+6


source


First, you need to connect the navbar to the IBOutlet so you can refer to it in your code. After that, this code should work:

    let navigationItem = UINavigationItem(title: "Title")
    navigationItem.rightBarButtonItem = self.addButton
    navigationItem.hidesBackButton = true
    self.navigationBar.pushItem(navigationItem, animated: false)

      

+1


source


Swift 4.2;

Add to viewController

override func viewDidLoad() {
        super.viewDidLoad()
        self.addNavigationBarButton(imageName: "ic_back", direction:.left)
 }

      

Add Class

your API or utility class

public func addNavigationBarButton(imageName:String,direction:direction){
    var image = UIImage(named: imageName)
    image = image?.withRenderingMode(.alwaysOriginal)
    switch direction {
    case .left:
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: #selector(goBack))
    case .right:
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: #selector(goBack))
    }
}

@objc public func goBack() {
    self.navigationController?.popViewController(animated: true)
}

public enum direction {
    case right
    case left
}

      

0


source


tested in Xcode 10.2, swift 5.0; First, I have embedded my ViewController in a UINavigationController in IB. Then in ViewDidLoad include these lines

self.title = "orange"
 self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(changeLayout)).

      

Note. Failed to access title or add button through navigation controller. For example: setting the title - Self.navigationcontroller.navigationItem.title doesn't work,

0


source







All Articles