// // UIBarButtonItem+Custom.swift // FireBoltt // // Created by lemo on 2018/5/16. // Copyright © 2020年 ecell. All rights reserved. // import UIKit fileprivate struct Metric { static let itemMargin: CGFloat = 11 - 8 // 系统边距 6P:20 其余:16 static let systemMargin: CGFloat = (-kNavBarItemMargin + Metric.itemMargin) static let ios11Imageinset: CGFloat = -(5.0 + 8.0) } extension UIBarButtonItem { /// 导航栏按钮(图片) /// /// - Parameters: /// - direction: true: 左边 false: 右边 static func barButtonItem(_ normalImageName: String, _ highImageName: String?, _ target: Any?, _ action: Selector, _ direction: Bool) -> [UIBarButtonItem] { let button = UIButton(type: .custom) let normalImage = UIImage(named: normalImageName) button.setImage(normalImage, for: .normal) button.frame.size = (normalImage?.size)! if highImageName != nil { button.setImage(UIImage(named: highImageName!), for: .highlighted) } button.addTarget(target, action: action, for: .touchUpInside) // IOS11 fixedSpace 失效,使用此方法 if kIOS11 { button.imageEdgeInsets = direction ? UIEdgeInsets(top: 0, left: Metric.ios11Imageinset, bottom: 0, right: 0) : UIEdgeInsets(top: 0, left: 0, bottom: 0, right: Metric.ios11Imageinset) button.contentHorizontalAlignment = direction ? .left : .right return [UIBarButtonItem(customView: button)] } // 按钮间距为6,6P间距为20,6位16 let fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) fixedSpace.width = Metric.systemMargin return [fixedSpace, UIBarButtonItem(customView: button)] } /// 常用导航栏按钮(图片 + 文字) /// /// - Parameters: /// - direction: true: 左边 false: 右边 static func baseBarButtonItem(normalImg: String?, highImg: String?, title: String?, target: Any?, action: Selector?) -> UIBarButtonItem { let button = UIButton(type: .custom) if let normalImg = normalImg { button.setImage(UIImage(named: normalImg), for: .normal) } if let highImg = highImg { button.setImage(UIImage(named: highImg), for: .highlighted) } button.setTitle(title, for: .normal) button.titleLabel?.font = SystemMediumFont(18) button.setTitleColor(ThemeManagerFrieBoltt.commonTextColor, for: .normal) if let action = action { button.addTarget(target, action: action, for: .touchUpInside) } return UIBarButtonItem(customView: button) } }