Guest User

Untitled

a guest
Jan 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. import UIKit
  2.  
  3. extension UIBarButtonItem {
  4.  
  5. convenience init (image: UIImage?, target: Any?, action: Selector?) {
  6. let button = UIButton(type: .custom)
  7. button.imageView?.contentMode = .scaleAspectFit
  8. button.frame = CGRect(x: 0.0, y: 0.0, width: 44.0, height: 44.0)
  9. button.addTarget(target, action: action!, for: .touchUpInside)
  10. button.setImage(image, for: UIControlState.normal)
  11. button.imageEdgeInsets = UIEdgeInsets(top: 7.0, left: 7.0, bottom: 7.0, right: 7.0)
  12.  
  13. if #available(iOS 9.0, *) {
  14. button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
  15. button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
  16. }
  17. self.init(customView: button)
  18. }
  19. }
  20.  
  21. let rightButton = UIBarButtonItem(image: UIImage(named: "example"), target: self, action: #selector(exampleAction(sender:)))
  22. self.navigationItem.rightBarButtonItem = rightButton
Add Comment
Please, Sign In to add comment