UIBarButtonItem+GKExtension.swift
3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// UIBarButtonItem+GKExtension.swift
// GKNavigationBarSwift
//
// Created by QuintGao on 2020/3/25.
// Copyright © 2020 QuintGao. All rights reserved.
//
import UIKit
extension UIBarButtonItem {
public class func gk_item(title: String?, target: Any, action: Selector) -> UIBarButtonItem {
gk_item(title: title, image: nil, target: target, action: action)
}
public class func gk_item(title: String?, font: UIFont?, target: Any, action: Selector) -> UIBarButtonItem {
gk_item(title: title, color: nil, font: font, target: target, action: action)
}
public class func gk_item(title: String?, color: UIColor?, target: Any, action: Selector) -> UIBarButtonItem {
gk_item(title: title, color: color, font: nil, target: target, action: action)
}
public class func gk_item(title: String?, color: UIColor?, font: UIFont?, target: Any, action: Selector) -> UIBarButtonItem {
gk_item(title: title, titleColor: color, font: font, image: nil, imageColor: nil, highLightImage: nil, target: target, action: action)
}
public class func gk_item(image: UIImage?, target: Any, action: Selector) -> UIBarButtonItem {
gk_item(title: nil, image: image, target: target, action: action)
}
public class func gk_item(image: UIImage?, color: UIColor?, target: Any, action: Selector) -> UIBarButtonItem {
gk_item(title: nil, titleColor: nil, font: nil, image: image, imageColor: color, highLightImage: nil, target: target, action: action)
}
public class func gk_item(image: UIImage?, highLightImage: UIImage?, target: Any, action: Selector) -> UIBarButtonItem {
gk_item(title: nil, titleColor: nil, font: nil, image: image, imageColor: nil, highLightImage: highLightImage, target: target, action: action)
}
public class func gk_item(title: String?, image: UIImage?, target: Any, action: Selector) -> UIBarButtonItem {
gk_item(title: title, titleColor: nil, font: nil, image: image, imageColor: nil, highLightImage: nil, target: target, action: action)
}
public class func gk_item(title: String?, titleColor: UIColor?, font: UIFont?, image: UIImage?, imageColor: UIColor?, highLightImage: UIImage?, target: Any, action: Selector) -> UIBarButtonItem {
let button = UIButton()
if let title = title {
button.setTitle(title, for: .normal)
}
if let titleColor = titleColor {
button.setTitleColor(titleColor, for: .normal)
}
if let font = font {
button.titleLabel?.font = font
}
var buttonImage = image
if let image = image, let imageColor = imageColor {
buttonImage = UIImage.gk_change(with: image, color: imageColor)
}
if let buttonImage = buttonImage {
button.setImage(buttonImage, for: .normal)
}
if let highLightImage = highLightImage {
button.setImage(highLightImage, for: .highlighted)
}
button.sizeToFit()
if button.bounds.size.width < 44.0 {
button.bounds = CGRect(x: 0, y: 0, width: 44.0, height: 44.0)
}
button.addTarget(target, action: action, for: .touchUpInside)
return UIBarButtonItem(customView: button)
}
}