UIBarButtonItem
public extension UIBarButtonItem
Closures
framework adds closures for UIBarButtonItem
tap events, usually
found in a UINavigationBar.
All initializers that support the target-action pattern now have an equivalent
initialier that contains a handler
parameter.
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Left item", style: .plain) {
// left bar button item tapped
}
To add the closure handler to an existing UIBarButtonItem
, simply call the
onTap(handler:)
method. For instance, if you created your button
in a storyboard, you could call the following in your viewDidLoad
method.
let myRightBarButton = navigationItem.rightBarButtonItem!
myRightBarButton.onTap {
// right bar button item tapped
}
-
A convenience initializer for a UIBarButtonItem so that the tap event can be handled with a closure. This is equivalent of using the init(image:style:target:action:) initializer.
Declaration
Swift
convenience init(image: UIImage?, style: UIBarButtonItem.Style, handler: @escaping () -> Void)
Parameters
image
The image to use for the button
style
The
UIBarButtonItemStyle
of the buttonhandler
The closure that is called when the button is tapped
-
A convenience initializer for a UIBarButtonItem so that the tap event can be handled with a closure. This is equivalent of using the init(image:landscapeImagePhone:style:target:action:) initializer.
Declaration
Swift
@available(iOS 5.0, *) convenience init(image: UIImage?, landscapeImagePhone: UIImage?, style: UIBarButtonItem.Style, handler: @escaping () -> Void)
Parameters
image
The image to use for the button
landscapeImagePhone
The image to use for the compressed landscape bar item
style
The
UIBarButtonItemStyle
of the buttonhandler
The closure that is called when the button is tapped
-
A convenience initializer for a UIBarButtonItem so that the tap event can be handled with a closure. This is equivalent of using the init(title:style:target:action:) initializer.
Declaration
Swift
convenience init(title: String?, style: UIBarButtonItem.Style, handler: @escaping () -> Void)
Parameters
title
The text to use for the button
style
The
UIBarButtonItemStyle
of the buttonhandler
The closure that is called when the button is tapped
-
A convenience initializer for a UIBarButtonItem so that the tap event can be handled with a closure. This is equivalent of using the init(barButtonSystemItem:target:action:) initializer.
Declaration
Swift
convenience init(barButtonSystemItem systemItem: UIBarButtonItem.SystemItem, handler: @escaping () -> Void)
Parameters
barButtonSystemItem
The
UIBarButtonSystemItem
to be used for the buttonhandler
The closure that is called when the button is tapped
-
This method is a convenience method to add a closure handler to a
UIBarButtonItem
. Use this method if you are creating aUIBarButtonItem
using an initializer other than the convience ones provide, or if the item was created by a Storyboard or xib.
An example that adds a closure handler to an existing
UIBarButtonItem
:myBarButtonItem.onTap { // bar button tapped code }
Declaration
Swift
func onTap(handler: @escaping () -> Void)
Parameters
handler
The closure that will be called when the tap is recognized.