I have a bar button item set to a custom image from my image assets folder. After launch, once the user presses the button, I want it to change to a system item—the stop (X
) button. I know how to programmatically change the image of the bar button, but how do I change the image to one of the system items in my code?
Swift 3, Xcode 8 beta 1.
Edit Here are the system items I'm referencing:
How can I change my button's image to one of those system items from my code?
navigationItem.rightBarButtonItem = UIBarButtonItem(
barButtonSystemItem: UIBarButtonItem.SystemItem.add,
target: self,
action: #selector(yourMethod)
)
Change 'add' according to https://developer.apple.com/documentation/uikit/uibarbuttonitem/systemitem
For those of you using iOS 13 & up, you can use the UIImage(systemName:) property. You can search the systemItem names and corresponding images using Apple's SF Symbols app. Here's an example using the stop X button:
barButtonItem.image = UIImage(systemName: "xmark")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With