@@ -14,7 +14,7 @@ import UIKit
14
14
of the optional SwiftMessages protocols and provides some convenience functions
15
15
and a configurable tap handler. Message views do not need to inherit from `BaseVew`.
16
16
*/
17
- open class BaseView : UIView , BackgroundViewable , MarginAdjustable , MessageSizeable {
17
+ open class BaseView : UIView , BackgroundViewable , MarginAdjustable , HasBoundaryInsets {
18
18
19
19
/*
20
20
MARK: - IB outlets
@@ -97,28 +97,20 @@ open class BaseView: UIView, BackgroundViewable, MarginAdjustable, MessageSizeab
97
97
}
98
98
addSubview ( backgroundView)
99
99
self . backgroundView = backgroundView
100
- NSLayoutConstraint . activate ( [
101
- backgroundView. centerXAnchor. constraint ( equalTo: centerXAnchor)
102
- . with ( priority: . belowMessageSizeable) ,
103
- backgroundView. topAnchor. constraint (
104
- equalTo: layoutMarginsGuide. topAnchor,
105
- constant: insets. top
106
- ) . with ( priority: . belowMessageSizeable) ,
107
- backgroundView. bottomAnchor. constraint (
108
- equalTo: layoutMarginsGuide. bottomAnchor,
109
- constant: - insets. bottom
110
- ) . with ( priority: . belowMessageSizeable) ,
111
- backgroundView. heightAnchor. constraint ( equalToConstant: 350 )
112
- . with ( priority: UILayoutPriority ( rawValue: 200 ) ) ,
113
- backgroundView. leftAnchor. constraint (
114
- equalTo: layoutMarginsGuide. leftAnchor,
115
- constant: insets. left
116
- ) . with ( priority: . belowMessageSizeable) ,
117
- backgroundView. rightAnchor. constraint (
118
- equalTo: layoutMarginsGuide. rightAnchor,
119
- constant: - insets. right
120
- ) . with ( priority: . belowMessageSizeable) ,
121
- ] )
100
+ backgroundView. centerXAnchor. constraint ( equalTo: centerXAnchor) . with ( priority: UILayoutPriority ( rawValue: 950 ) ) . isActive = true
101
+ backgroundView. topAnchor. constraint ( equalTo: layoutMarginsGuide. topAnchor, constant: insets. top) . with ( priority: UILayoutPriority ( rawValue: 900 ) ) . isActive = true
102
+ backgroundView. bottomAnchor. constraint ( equalTo: layoutMarginsGuide. bottomAnchor, constant: - insets. bottom) . with ( priority: UILayoutPriority ( rawValue: 900 ) ) . isActive = true
103
+ backgroundView. heightAnchor. constraint ( equalToConstant: 350 ) . with ( priority: UILayoutPriority ( rawValue: 200 ) ) . isActive = true
104
+ layoutConstraints = [
105
+ backgroundView. leftAnchor. constraint ( equalTo: layoutMarginsGuide. leftAnchor, constant: insets. left) . with ( priority: UILayoutPriority ( rawValue: 900 ) ) ,
106
+ backgroundView. rightAnchor. constraint ( equalTo: layoutMarginsGuide. rightAnchor, constant: - insets. right) . with ( priority: UILayoutPriority ( rawValue: 900 ) ) ,
107
+ ]
108
+ regularWidthLayoutConstraints = [
109
+ backgroundView. leftAnchor. constraint ( greaterThanOrEqualTo: layoutMarginsGuide. leftAnchor, constant: insets. left) . with ( priority: UILayoutPriority ( rawValue: 900 ) ) ,
110
+ backgroundView. rightAnchor. constraint ( lessThanOrEqualTo: layoutMarginsGuide. rightAnchor, constant: - insets. right) . with ( priority: UILayoutPriority ( rawValue: 900 ) ) ,
111
+ backgroundView. widthAnchor. constraint ( lessThanOrEqualToConstant: 500 ) . with ( priority: UILayoutPriority ( rawValue: 950 ) ) ,
112
+ backgroundView. widthAnchor. constraint ( equalToConstant: 500 ) . with ( priority: UILayoutPriority ( rawValue: 200 ) ) ,
113
+ ]
122
114
installTapRecognizer ( )
123
115
}
124
116
@@ -139,23 +131,20 @@ open class BaseView: UIView, BackgroundViewable, MarginAdjustable, MessageSizeab
139
131
}
140
132
addSubview ( backgroundView)
141
133
self . backgroundView = backgroundView
142
- NSLayoutConstraint . activate ( [
143
- backgroundView. centerXAnchor. constraint ( equalTo: centerXAnchor)
144
- . with ( priority: . belowMessageSizeable) ,
145
- backgroundView. topAnchor. constraint ( equalTo: topAnchor, constant: insets. top)
146
- . with ( priority: . required) ,
147
- backgroundView. bottomAnchor. constraint ( equalTo: bottomAnchor, constant: - insets. bottom)
148
- . with ( priority: . required) ,
149
- backgroundView. heightAnchor. constraint ( equalToConstant: 350 )
150
- . with ( priority: UILayoutPriority ( rawValue: 200 ) ) ,
151
- backgroundView. leftAnchor. constraint (
152
- equalTo: layoutMarginsGuide. leftAnchor, constant: insets. left
153
- ) . with ( priority: . belowMessageSizeable) ,
154
- backgroundView. rightAnchor. constraint (
155
- equalTo: layoutMarginsGuide. rightAnchor,
156
- constant: - insets. right
157
- ) . with ( priority: . belowMessageSizeable) ,
158
- ] )
134
+ backgroundView. centerXAnchor. constraint ( equalTo: centerXAnchor) . with ( priority: UILayoutPriority ( rawValue: 950 ) ) . isActive = true
135
+ backgroundView. topAnchor. constraint ( equalTo: topAnchor, constant: insets. top) . with ( priority: UILayoutPriority ( rawValue: 1000 ) ) . isActive = true
136
+ backgroundView. bottomAnchor. constraint ( equalTo: bottomAnchor, constant: - insets. bottom) . with ( priority: UILayoutPriority ( rawValue: 1000 ) ) . isActive = true
137
+ backgroundView. heightAnchor. constraint ( equalToConstant: 350 ) . with ( priority: UILayoutPriority ( rawValue: 200 ) ) . isActive = true
138
+ layoutConstraints = [
139
+ backgroundView. leftAnchor. constraint ( equalTo: layoutMarginsGuide. leftAnchor, constant: insets. left) . with ( priority: UILayoutPriority ( rawValue: 900 ) ) ,
140
+ backgroundView. rightAnchor. constraint ( equalTo: layoutMarginsGuide. rightAnchor, constant: - insets. right) . with ( priority: UILayoutPriority ( rawValue: 900 ) ) ,
141
+ ]
142
+ regularWidthLayoutConstraints = [
143
+ backgroundView. leftAnchor. constraint ( greaterThanOrEqualTo: layoutMarginsGuide. leftAnchor, constant: insets. left) . with ( priority: UILayoutPriority ( rawValue: 900 ) ) ,
144
+ backgroundView. rightAnchor. constraint ( lessThanOrEqualTo: layoutMarginsGuide. rightAnchor, constant: - insets. right) . with ( priority: UILayoutPriority ( rawValue: 900 ) ) ,
145
+ backgroundView. widthAnchor. constraint ( lessThanOrEqualToConstant: 500 ) . with ( priority: UILayoutPriority ( rawValue: 950 ) ) ,
146
+ backgroundView. widthAnchor. constraint ( equalToConstant: 500 ) . with ( priority: UILayoutPriority ( rawValue: 200 ) ) ,
147
+ ]
159
148
installTapRecognizer ( )
160
149
}
161
150
@@ -201,14 +190,6 @@ open class BaseView: UIView, BackgroundViewable, MarginAdjustable, MessageSizeab
201
190
return super. point ( inside: point, with: event)
202
191
}
203
192
204
- // MARK: - MessageSizeable
205
-
206
- /// Configure the view's size
207
- public var messageSize = MessageSize ( )
208
-
209
- /// Configure the view's insets from the container
210
- public var messageInsets = MessageInsets ( )
211
-
212
193
/*
213
194
MARK: - MarginAdjustable
214
195
@@ -248,6 +229,13 @@ open class BaseView: UIView, BackgroundViewable, MarginAdjustable, MessageSizeab
248
229
249
230
@IBInspectable open var bounceAnimationOffset : CGFloat = 5
250
231
232
+ /*
233
+ MARK: - HasBoundaryInsets
234
+ */
235
+
236
+ /// Configure the view's inset from the superview or save area
237
+ public var boundaryInsets = BoundaryInsets ( )
238
+
251
239
/*
252
240
MARK: - Setting the height
253
241
*/
@@ -276,6 +264,29 @@ open class BaseView: UIView, BackgroundViewable, MarginAdjustable, MessageSizeab
276
264
}
277
265
278
266
private var backgroundHeightConstraint : NSLayoutConstraint ?
267
+
268
+ /*
269
+ Mark: - Layout
270
+ */
271
+
272
+ open override func updateConstraints( ) {
273
+ super. updateConstraints ( )
274
+ let on : [ NSLayoutConstraint ]
275
+ let off : [ NSLayoutConstraint ]
276
+ switch traitCollection. horizontalSizeClass {
277
+ case . regular:
278
+ on = regularWidthLayoutConstraints
279
+ off = layoutConstraints
280
+ default :
281
+ on = layoutConstraints
282
+ off = regularWidthLayoutConstraints
283
+ }
284
+ on. forEach { $0. isActive = true }
285
+ off. forEach { $0. isActive = false }
286
+ }
287
+
288
+ private var layoutConstraints : [ NSLayoutConstraint ] = [ ]
289
+ private var regularWidthLayoutConstraints : [ NSLayoutConstraint ] = [ ]
279
290
}
280
291
281
292
/*
0 commit comments