You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

67 lines
2.6 KiB

//
// UIBarButtonItem+Custom.swift
// Lookfit
//
// Created by lemo on 2018/5/16.
// Copyright © 2020 ecell. All rights reserved.
//
import UIKit
fileprivate struct Metric {
static let itemMargin: CGFloat = 11 - 8
// 6P20 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)]
}
// 66P20616
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(ThemeManager.commonTextColor, for: .normal)
if let action = action {
button.addTarget(target, action: action, for: .touchUpInside)
}
return UIBarButtonItem(customView: button)
}
}