-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathRNAddToWalletButtonView.swift
More file actions
66 lines (52 loc) · 1.59 KB
/
Copy pathRNAddToWalletButtonView.swift
File metadata and controls
66 lines (52 loc) · 1.59 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
import Foundation
import PassKit
import UIKit
@objc(RNAddToWalletButtonView)
class RNAddToWalletButtonView: UIView {
private var addPassButton = PKAddPassButton(addPassButtonStyle: .blackOutline)
private let defaultWidth: CGFloat = 120
private let defaultHeight: CGFloat = 40
@objc var buttonStyle: NSString = "" {
didSet {
updateButtonStyle()
}
}
@objc var borderRadius: CGFloat = 4.0 {
didSet {
applyBorderRadius()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupButton()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupButton()
}
private func setupButton() {
addSubview(addPassButton)
updateButtonStyle()
}
private func applyBorderRadius() {
let maxRadius = addPassButton.bounds.height / 2
let radius = min(borderRadius, maxRadius)
addPassButton.layer.cornerRadius = radius
addPassButton.layer.masksToBounds = true
addPassButton.clipsToBounds = true
}
override func layoutSubviews() {
super.layoutSubviews()
let width = bounds.width > 0 ? bounds.width : defaultWidth
let height = bounds.height > 0 ? bounds.height : defaultHeight
addPassButton.frame = CGRect(x: 0, y: 0, width: width, height: height)
applyBorderRadius()
}
private func updateButtonStyle() {
let style: PKAddPassButtonStyle = (buttonStyle.lowercased == "black") ? .black : .blackOutline
addPassButton.removeFromSuperview()
addPassButton = PKAddPassButton(addPassButtonStyle: style)
addSubview(addPassButton)
setNeedsLayout()
}
}