Reputation: 370
I am facing a strange issue in my App.
I initialised a UIBarButton, added the action, custom Image to it and then added it as rightBarButtonItem of navigationController.
The click action is working as expected on iOS 12.4.1 but fails on iOS 13.3.1 . My code is shown below.
let syncButton = UIBarButton()
syncButton.action = #selector(someMethod) // here someMethod is the method I want to execute on the click
self.navigationItem.rightBarButton = syncButton
Upvotes: 2
Views: 178
Reputation: 613
Swift 5
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
OR
let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
let play = UIBarButtonItem(title: "Play", style: .plain, target: self, action: #selector(playTapped))
navigationItem.rightBarButtonItems = [add, play]
Upvotes: 0
Reputation:
You can try this code, Hope its working fine
let buttonFrame = CGRect(x: 0, y: 0, width: 33, height: 33)
let btnSideMenu = UIButton(frame: buttonFrame)
btnSideMenu.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
btnSideMenu.setImage(#imageLiteral(resourceName: "burger"), for: .normal)
btnSideMenu.addTarget(self, action: #selector(btnSideMenuTapped), for: .touchUpInside)
let leftBarButton = UIBarButtonItem(customView: btnSideMenu)
self.navigationItem.setLeftBarButton(leftBarButton, animated: true)
Upvotes: 0
Reputation: 100503
Assign the target to self
syncButton.target = self
OR
customBu.addTarget(self, action: #selector(someMethod), for: .touchUpInside)
and set it as a customView
Upvotes: 3