iosswiftuibarbuttonitem

How to put the burger icon in a UIBarButtonItem?


I want to use burger icon i.e.

enter image description here

Basically I don't know how to put the burger icon in a UIBarButtonItem, and only exists in System item some icons and not exists what I want.


Solution

  • You'll want to use the UIBarButtonItem constructor init(image: UIImage?, style: UIBarButtonItemStyle, target: Any?, action: Selector?)

    an example usage would be:

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "burger_icon"), style: .plain, target: self, action: #selector(self.someMethod))
    

    In this example "burger_icon" is the name of the image asset in your project and self.someMethod is the method that is called when this button is tapped.

    For more information on the UIBarButtonItem class you can look at the Apple documentation for the class. https://developer.apple.com/reference/uikit/uibarbuttonitem