Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- extension UIBarButtonItem {
- convenience init (image: UIImage?, target: Any?, action: Selector?) {
- let button = UIButton(type: .custom)
- button.imageView?.contentMode = .scaleAspectFit
- button.frame = CGRect(x: 0.0, y: 0.0, width: 44.0, height: 44.0)
- button.addTarget(target, action: action!, for: .touchUpInside)
- button.setImage(image, for: UIControlState.normal)
- button.imageEdgeInsets = UIEdgeInsets(top: 7.0, left: 7.0, bottom: 7.0, right: 7.0)
- if #available(iOS 9.0, *) {
- button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
- button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
- }
- self.init(customView: button)
- }
- }
- let rightButton = UIBarButtonItem(image: UIImage(named: "example"), target: self, action: #selector(exampleAction(sender:)))
- self.navigationItem.rightBarButtonItem = rightButton
Add Comment
Please, Sign In to add comment