Swift Navigation Bar item not calling action
Try something like this
If you have a parameter
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "ButtonName", style: .done, target: self, action: #selector(YourViewController.yourAction(_:)))
}
Without parameter
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "ButtonName", style: .done, target: self, action: #selector(YourViewController.yourAction))
}
Button action does not react when .touchUpInside inside a navigation bar
Instead of creating a title view and measuring its size and stuff... there are a load of convenience functions for doing this...
Take a look here... https://www.hackingwithswift.com/example-code/uikit/how-to-add-a-bar-button-to-a-navigation-bar
You can do something like...
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(didTapProfile))
There are different ways to create UIBarButtonItem
with images and text also...
https://developer.apple.com/documentation/uikit/uibarbuttonitem
UIButton in Navigation Bar Not Recognizing Taps in iOS 11
I found a solution from another developer.
You have to add width and height constraints to the view that wraps all your stuff inside the bar button item. So in my case, the item labeled View
needs the constraints.
Here's what to do:
Step 1
Create an @IBOutlet
from View
to the view controller that has this nav bar.
@IBOutlet weak var myView: UIView!
Step 2
In the containing view controller, inside viewDidLoad()
add the constraints:
myView.widthAnchor.constraint(equalToConstant: 63).isActive = true
myView.heightAnchor.constraint(equalToConstant: 33).isActive = true
After that, everything works like it used to.
Right Bar Button item not working
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 50, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:0];
// create a standard BarButtonItem
UIBarButtonItem *SettingsBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_setting.png"] style:UIBarButtonItemStylePlain
target:self
action:@selector(ActionMethod:)];
[buttons addObject:SettingsBtn];
[SettingsBtn release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithCustomView:toolbar]autorelease];
[toolbar release];
Execute action when back bar button of UINavigationController is pressed
One option would be implementing your own custom back button. You would need to add the following code to your viewDidLoad method:
- (void) viewDidLoad {
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(back:)];
self.navigationItem.leftBarButtonItem = newBackButton;
}
- (void) back:(UIBarButtonItem *)sender {
// Perform your custom actions
// ...
// Go back to the previous ViewController
[self.navigationController popViewControllerAnimated:YES];
}
UPDATE:
Here is the version for Swift:
override func viewDidLoad {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Bordered, target: self, action: "back:")
self.navigationItem.leftBarButtonItem = newBackButton
}
@objc func back(sender: UIBarButtonItem) {
// Perform your custom actions
// ...
// Go back to the previous ViewController
self.navigationController?.popViewControllerAnimated(true)
}
UPDATE 2:
Here is the version for Swift 3:
override func viewDidLoad {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(YourViewController.back(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton
}
@objc func back(sender: UIBarButtonItem) {
// Perform your custom actions
// ...
// Go back to the previous ViewController
_ = navigationController?.popViewController(animated: true)
}
Navigation bar button doesn't show in navigationbar
let rightBarButton = UIBarButtonItem(image: UIImage(named: "test"), style: .plain, target: self, action: #selector(logoutButtonTapped))
self.navigationController?.navigationBar.topItem?.rightBarButtonItem = rightBarButton
now this is works for me
UIBarButtonItem: target-action not working?
I do not think the target and action of the UIBarButtonItem apply to custom views. Try using a UIButton instead of UIImageView and applying the target and action to the button.
Sample code in Swift:
let button = UIButton(type: .Custom)
if let image = UIImage(named:"icon-menu.png") {
button.setImage(image, forState: .Normal)
}
button.frame = CGRectMake(0.0, 0.0, 30.0, 30.0)
button.addTarget(self, action: #selector(MyClass.myMethod), forControlEvents: .TouchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.leftBarButtonItem = barButton
Related Topics
Swift: How to Add a Class Method in 'string" Extension
Can't Load Images on MAC Screensaver Release Build (It Works on Xcode Debug Build)
How to Retrieve The Name of Participants in Msconversation
How to Assign a Generic Function to a Variable
Convert/Wrap Swift Struct as Nsvalue for Caanimation Purposes
What's The Difference Between [String] VS. [(String)]
Swift: Rsa Encrypt a String with a Specific Private Key
Swift Lose Precision in Decimal Formatting
Swift Playground with Debugger Support
More Precision Than Double in Swift
Easiest Way to Truncate Float to 2 Decimal Places
Swift Navigation Bar Item Not Calling Action
How to Use Crc32 from Zlib in Swift (Xcode 9)
Why Is The Leading Swipe Action Also Duplicated as a Trailing Action
Type 'Int' Does Not Conform to Protocol 'Booleantype'
+' Is Deprecated: Mixed-Type Addition Is Deprecated in Swift 3.1