Skip to content

Instantly share code, notes, and snippets.

@yycking
Created September 22, 2017 08:15
Show Gist options
  • Save yycking/4a1d77880eea2c8e0739bb87518c7ee4 to your computer and use it in GitHub Desktop.
Save yycking/4a1d77880eea2c8e0739bb87518c7ee4 to your computer and use it in GitHub Desktop.
Get UIBarButtonItem icon
extension UIBarButtonSystemItem {
func image() -> UIImage? {
let tempItem = UIBarButtonItem(barButtonSystemItem: self,
target: nil,
action: nil)
// add to toolbar and render it
let bar = UIToolbar()
bar.setItems([tempItem],
animated: false)
bar.snapshotView(afterScreenUpdates: true)
// got image from real uibutton
let itemView = tempItem.value(forKey: "view") as! UIView
for view in itemView.subviews {
if let button = view as? UIButton,
let image = button.imageView?.image {
return image
}
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment