Geen omschrijving

UIBarButtonItemExt.swift 2.5KB

    // // UIBarButtonItemExt.swift // ExtensionKit // // Created by FFIB on 2017/9/14. // Copyright © 2017年 FFIB. All rights reserved. // 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: 44)) 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: 2, 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) } }