1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import UIKit
- public extension UIBarButtonItem {
- convenience init(space: CGFloat) {
- self.init(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
- self.width = space
- }
- convenience init(image: UIImage?, target: Any, action: Selector) {
- let button = UIButton(type: .custom)
- button.frame = CGRect(x: 0, y: 0, width: 36, height: 36)
- button.setImage(image, for: .normal)
- button.addTarget(target, action: action, for: .touchDown)
- self.init(customView: button)
- }
- convenience init(title: String, target: Any, action: Selector) {
- let button = UIButton(type: .custom)
- button.frame = CGRect(x: 0, y: 0, width: 36, height: 36)
- button.setTitle(title, for: .normal)
- button.addTarget(target, action: action, for: .touchDown)
- button.sizeToFit()
- self.init(customView: button)
- }
- convenience init(titles: [String], btnSpace: CGFloat, target: Any, actions: [Selector]) {
- let barView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 36))
- var lastX: CGFloat = 0
- for (title, action) in zip(titles, actions) {
- let button = UIButton(type: .custom)
- button.frame = CGRect(x: lastX, y: 2, width: 36, height: 36)
- button.setTitle(title, for: .normal)
- button.addTarget(target, action: action, for: .touchDown)
- button.sizeToFit()
- button.center = CGPoint(x: button.center.x, y: barView.center.y)
- barView.addSubview(button)
- lastX += button.width + btnSpace
- }
- barView.width = lastX
- self.init(customView: barView)
- }
- convenience init(images: [UIImage?], btnSpace: CGFloat = 0, targets: [Any], actions: [Selector]) {
- let barView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 36))
- var lastX: CGFloat = 0
- for (target, (image, action)) in zip(targets, zip(images, actions)) {
- let button = UIButton(type: .custom)
- button.frame = CGRect(x: lastX, y: 0, width: 36, height: 36)
- button.setImage(image, for: .normal)
- button.addTarget(target, action: action, for: .touchDown)
- barView.addSubview(button)
- lastX += button.width + btnSpace
- }
- barView.width = lastX - btnSpace
- self.init(customView: barView)
- }
- }
|