@@ -27,11 +27,12 @@ open class UINotificationView: UIView {
27
27
private let containerStackViewDefaultSpacing : CGFloat = 14
28
28
29
29
private lazy var containerStackView : UIStackView = {
30
- let stackView = UIStackView ( arrangedSubviews : [ self . imageView , self . titlesStackView , self . chevronImageView ] )
30
+ let stackView = UIStackView ( )
31
31
stackView. axis = . horizontal
32
32
stackView. spacing = self . containerStackViewDefaultSpacing
33
33
stackView. translatesAutoresizingMaskIntoConstraints = false
34
34
stackView. alignment = . center
35
+
35
36
return stackView
36
37
} ( )
37
38
@@ -71,6 +72,13 @@ open class UINotificationView: UIView {
71
72
return imageView
72
73
} ( )
73
74
75
+ var button : UIButton ? {
76
+ let button = self . notification. button
77
+ button? . translatesAutoresizingMaskIntoConstraints = false
78
+
79
+ return button
80
+ }
81
+
74
82
// MARK: Gestures
75
83
internal fileprivate( set) lazy var panGestureRecognizer : UIPanGestureRecognizer = { [ unowned self] in
76
84
let gesture = UIPanGestureRecognizer ( )
@@ -104,6 +112,7 @@ open class UINotificationView: UIView {
104
112
105
113
layoutMargins = UIEdgeInsets ( top: 4 , left: 0 , bottom: 4 , right: 0 )
106
114
115
+ addArrangedSubviewsForContainerStackView ( )
107
116
addSubview ( containerStackView)
108
117
109
118
setupConstraints ( )
@@ -124,9 +133,6 @@ open class UINotificationView: UIView {
124
133
imageView. image = notification. content. image
125
134
imageView. isHidden = notification. content. image == nil
126
135
127
- let chevronImageWidth = chevronImageView. image? . size. width ?? 0
128
- containerStackView. spacing = imageView. isHidden ? - chevronImageWidth : containerStackViewDefaultSpacing
129
-
130
136
backgroundColor = notification. style. backgroundColor
131
137
132
138
chevronImageView. tintColor = notification. style. titleTextColor
@@ -136,6 +142,15 @@ open class UINotificationView: UIView {
136
142
tapGestureRecognizer. isEnabled = notification. style. interactive
137
143
}
138
144
145
+ open func updateForButton( ) {
146
+ guard !subviews. isEmpty else { return }
147
+ subviews. forEach { subview in
148
+ subview. removeFromSuperview ( )
149
+ }
150
+
151
+ setupView ( )
152
+ }
153
+
139
154
/// Called when all constraints should be setup for the notification. Can be overwritten to set your own constraints.
140
155
/// When setting your own constraints, you should not be calling super.
141
156
open func setupConstraints( ) {
@@ -150,10 +165,28 @@ open class UINotificationView: UIView {
150
165
imageView. widthAnchor. constraint ( equalToConstant: 31 ) ,
151
166
imageView. heightAnchor. constraint ( equalToConstant: 31 )
152
167
]
168
+
169
+ button? . setContentHuggingPriority ( . defaultHigh, for: . horizontal)
153
170
154
171
NSLayoutConstraint . activate ( constraints)
155
172
}
156
173
174
+ private func addArrangedSubviewsForContainerStackView( ) {
175
+ var arrangedSubviews = [ self . imageView, self . titlesStackView, self . chevronImageView]
176
+
177
+ if let button = self . button {
178
+ arrangedSubviews. insert ( button, at: arrangedSubviews. count - 1 )
179
+ }
180
+
181
+ containerStackView. arrangedSubviews. forEach { ( view) in
182
+ containerStackView. removeArrangedSubview ( view)
183
+ }
184
+
185
+ arrangedSubviews. forEach { ( view) in
186
+ containerStackView. addArrangedSubview ( view)
187
+ }
188
+ }
189
+
157
190
@objc internal func handleTapGestureRecognizer( ) {
158
191
guard let presenter = presenter, presenter. state == UINotificationPresenterState . presented else { return }
159
192
notification. action? . execute ( )
@@ -184,9 +217,15 @@ open class UINotificationView: UIView {
184
217
}
185
218
186
219
extension UINotificationView : UINotificationDelegate {
220
+
187
221
func didUpdateContent( in notificaiton: UINotification ) {
188
222
updateForNotificationData ( )
189
223
}
224
+
225
+ func didUpdateButton( in notificaiton: UINotification ) {
226
+ updateForButton ( )
227
+ }
228
+
190
229
}
191
230
192
231
extension NSLayoutConstraint {
0 commit comments