Skip to content

Commit 04bfea0

Browse files
committed
Merge branch 'master' into develop
2 parents bc5d23b + c6dc616 commit 04bfea0

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// ESUIBarButtonItem+Extension.swift
3+
// ESUIHelper
4+
//
5+
// Created by Emil Karimov on 12/06/2020
6+
// Copyright © 2020 ESKARIA LLC. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
public struct ESBarButtonItem {
12+
let icon: UIImage?
13+
let target: AnyObject?
14+
let action: Selector?
15+
let accessibilityName: String
16+
}
17+
18+
extension UIBarButtonItem {
19+
20+
/// Get compacted barbutton items
21+
///
22+
/// - Parameter items: array of struct for buttons
23+
/// - Returns: only one barButton item
24+
public static func items(_ items: [DHBarButtonItem]) -> UIBarButtonItem {
25+
26+
let stackView = UIStackView()
27+
stackView.axis = .horizontal
28+
stackView.distribution = .fillEqually
29+
stackView.spacing = 0
30+
stackView.frame = CGRect(x: 0, y: 0, width: 44 * items.count, height: 44)
31+
32+
for item in items {
33+
34+
let button = UIButton(type: .custom)
35+
let image = item.icon
36+
button.setImage(image, for: .normal)
37+
if let target = item.target, let action = item.action {
38+
39+
button.addTarget(target, action: action, for: .touchUpInside)
40+
}
41+
button.accessibilityLabel = item.accessibilityName
42+
43+
button.heightAnchor.constraint(equalToConstant: 44).isActive = true
44+
button.widthAnchor.constraint(equalToConstant: 44).isActive = true
45+
stackView.addArrangedSubview(button)
46+
}
47+
48+
return UIBarButtonItem(customView: stackView)
49+
}
50+
51+
public static func item(icon: UIImage?, target: AnyObject?, action: Selector?, accessibilityName: String, width: CGFloat = 44, height: CGFloat = 44) -> UIBarButtonItem {
52+
53+
let button = UIButton(type: .custom)
54+
button.setImage(icon, for: .normal)
55+
if let target = target, let action = action {
56+
57+
button.addTarget(target, action: action, for: .touchUpInside)
58+
}
59+
button.accessibilityLabel = accessibilityName
60+
61+
if #available(iOS 11, *) {
62+
63+
button.heightAnchor.constraint(equalToConstant: height).isActive = true
64+
button.widthAnchor.constraint(equalToConstant: width).isActive = true
65+
66+
} else {
67+
68+
button.frame = CGRect(x: 0, y: 0, width: width, height: height)
69+
}
70+
let barButton = UIBarButtonItem(customView: button)
71+
return barButton
72+
}
73+
}

0 commit comments

Comments
 (0)