@@ -220,8 +220,10 @@ public class TZStackView: UIView {
220
220
stackViewConstraints += createMatchEdgesContraints ( arrangedSubviews)
221
221
stackViewConstraints += createFirstAndLastViewMatchEdgesContraints ( )
222
222
223
- if alignment == . FirstBaseline && axis == . Horizontal {
224
- stackViewConstraints. append ( constraint ( item: self , attribute: . Height, toItem: nil , attribute: . NotAnAttribute, priority: 49 ) )
223
+ if #available( iOS 8 , * ) {
224
+ if alignment == . FirstBaseline && axis == . Horizontal {
225
+ stackViewConstraints. append ( constraint ( item: self , attribute: . Height, toItem: nil , attribute: . NotAnAttribute, priority: 49 ) )
226
+ }
225
227
}
226
228
227
229
if distribution == . FillEqually {
@@ -255,8 +257,10 @@ public class TZStackView: UIView {
255
257
switch axis {
256
258
case . Horizontal:
257
259
stackViewConstraints. append ( constraint ( item: self , attribute: . Width, toItem: nil , attribute: . NotAnAttribute, priority: 49 ) )
258
- if alignment == . FirstBaseline {
259
- stackViewConstraints. append ( constraint ( item: self , attribute: . Height, toItem: nil , attribute: . NotAnAttribute, priority: 49 ) )
260
+ if #available( iOS 8 , * ) {
261
+ if alignment == . FirstBaseline {
262
+ stackViewConstraints. append ( constraint ( item: self , attribute: . Height, toItem: nil , attribute: . NotAnAttribute, priority: 49 ) )
263
+ }
260
264
}
261
265
case . Vertical:
262
266
stackViewConstraints. append ( constraint ( item: self , attribute: . Height, toItem: nil , attribute: . NotAnAttribute, priority: 49 ) )
@@ -281,8 +285,10 @@ public class TZStackView: UIView {
281
285
switch axis {
282
286
case . Horizontal:
283
287
stackViewConstraints. append ( constraint ( item: self , attribute: . Width, toItem: nil , attribute: . NotAnAttribute, priority: 49 ) )
284
- if alignment == . FirstBaseline {
285
- stackViewConstraints. append ( constraint ( item: self , attribute: . Height, toItem: nil , attribute: . NotAnAttribute, priority: 49 ) )
288
+ if #available( iOS 8 , * ) {
289
+ if alignment == . FirstBaseline {
290
+ stackViewConstraints. append ( constraint ( item: self , attribute: . Height, toItem: nil , attribute: . NotAnAttribute, priority: 49 ) )
291
+ }
286
292
}
287
293
case . Vertical:
288
294
stackViewConstraints. append ( constraint ( item: self , attribute: . Height, toItem: nil , attribute: . NotAnAttribute, priority: 49 ) )
@@ -313,17 +319,24 @@ public class TZStackView: UIView {
313
319
stackViewConstraints += createSurroundingSpacerViewConstraints ( spacerViews [ 0 ] , views: visibleArrangedSubviews)
314
320
}
315
321
316
- if layoutMarginsRelativeArrangement {
317
- if spacerViews. count > 0 {
318
- stackViewConstraints. append ( constraint ( item: self , attribute: . BottomMargin, toItem: spacerViews [ 0 ] ) )
319
- stackViewConstraints. append ( constraint ( item: self , attribute: . LeftMargin, toItem: spacerViews [ 0 ] ) )
320
- stackViewConstraints. append ( constraint ( item: self , attribute: . RightMargin, toItem: spacerViews [ 0 ] ) )
321
- stackViewConstraints. append ( constraint ( item: self , attribute: . TopMargin, toItem: spacerViews [ 0 ] ) )
322
+ if #available( iOS 8 , * ) {
323
+ if layoutMarginsRelativeArrangement {
324
+ if spacerViews. count > 0 {
325
+ stackViewConstraints. append ( constraint ( item: self , attribute: . BottomMargin, toItem: spacerViews [ 0 ] ) )
326
+ stackViewConstraints. append ( constraint ( item: self , attribute: . LeftMargin, toItem: spacerViews [ 0 ] ) )
327
+ stackViewConstraints. append ( constraint ( item: self , attribute: . RightMargin, toItem: spacerViews [ 0 ] ) )
328
+ stackViewConstraints. append ( constraint ( item: self , attribute: . TopMargin, toItem: spacerViews [ 0 ] ) )
329
+ }
322
330
}
323
331
}
324
332
}
325
333
326
- NSLayoutConstraint . activateConstraints ( subviewConstraints + stackViewConstraints)
334
+ let constraintsToActivate = subviewConstraints + stackViewConstraints
335
+ if #available( iOS 8 . 0 , * ) {
336
+ NSLayoutConstraint . activateConstraints ( constraintsToActivate)
337
+ } else {
338
+ addConstraints ( constraintsToActivate)
339
+ }
327
340
328
341
super. updateConstraints ( )
329
342
}
@@ -467,41 +480,50 @@ public class TZStackView: UIView {
467
480
private func createMatchEdgesContraints( views: [ UIView] ) - > [ NSLayoutConstraint] {
468
481
var constraints = [ NSLayoutConstraint] ( )
469
482
470
- switch axis {
471
- case . Horizontal:
472
- switch alignment {
473
- case . Fill:
474
- constraints += equalAttributes ( views: views, attribute: . Bottom)
475
- constraints += equalAttributes ( views: views, attribute: . Top)
476
- case . Center:
477
- constraints += equalAttributes ( views: views, attribute: . CenterY)
478
- case . Leading:
479
- constraints += equalAttributes ( views: views, attribute: . Top)
480
- case . Trailing:
481
- constraints += equalAttributes ( views: views, attribute: . Bottom)
482
- case . FirstBaseline:
483
- constraints += equalAttributes ( views: views, attribute: . FirstBaseline)
484
- case . LastBaseline:
483
+ switch ( alignment, axis) {
484
+ case ( . Fill, . Horizontal) : // Fill alignment
485
+ constraints += equalAttributes ( views: views, attribute: . Bottom)
486
+ constraints += equalAttributes ( views: views, attribute: . Top)
487
+ case ( . Fill, . Vertical) :
488
+ constraints += equalAttributes ( views: views, attribute: . Leading)
489
+ constraints += equalAttributes ( views: views, attribute: . Trailing)
490
+
491
+ case ( . Center, . Horizontal) : // Center alignment
492
+ constraints += equalAttributes ( views: views, attribute: . CenterY)
493
+ case ( . Center, . Vertical) :
494
+ constraints += equalAttributes ( views: views, attribute: . CenterX)
495
+
496
+ case ( . Leading, . Horizontal) : // Leading & Top alignment
497
+ constraints += equalAttributes ( views: views, attribute: . Top)
498
+ case ( . Leading, . Vertical) :
499
+ constraints += equalAttributes ( views: views, attribute: . Leading)
500
+
501
+ case ( . Trailing, . Horizontal) : // Trailing and Bottom alignment
502
+ constraints += equalAttributes ( views: views, attribute: . Bottom)
503
+ case ( . Trailing, . Vertical) :
504
+ constraints += equalAttributes ( views: views, attribute: . Trailing)
505
+
506
+ case ( . LastBaseline, . Horizontal) : // Last-Baseline alignment, works only on horizontal axis
507
+ if #available( iOS 8 , * ) {
485
508
constraints += equalAttributes ( views: views, attribute: . LastBaseline)
509
+ } else {
510
+ constraints += equalAttributes ( views: views, attribute: . Baseline)
486
511
}
487
-
488
- case . Vertical:
489
- switch alignment {
490
- case . Fill:
491
- constraints += equalAttributes ( views: views, attribute: . Leading)
492
- constraints += equalAttributes ( views: views, attribute: . Trailing)
493
- case . Center:
494
- constraints += equalAttributes ( views: views, attribute: . CenterX)
495
- case . Leading:
496
- constraints += equalAttributes ( views: views, attribute: . Leading)
497
- case . Trailing:
498
- constraints += equalAttributes ( views: views, attribute: . Trailing)
499
- case . FirstBaseline:
500
- constraints += [ ]
501
- case . LastBaseline:
512
+ case ( . LastBaseline, . Vertical) :
513
+ constraints += [ ]
514
+ default : break
515
+ }
516
+
517
+ if #available( iOS 8 , * ) { // First-Baseline alignment requires iOS 8+
518
+ switch ( alignment, axis) {
519
+ case ( . FirstBaseline, . Horizontal) : // First-Baseline alignment, works only on horizontal axis
520
+ constraints += equalAttributes ( views: views, attribute: . FirstBaseline)
521
+ case ( . FirstBaseline, . Vertical) :
502
522
constraints += [ ]
523
+ default : break
503
524
}
504
525
}
526
+
505
527
return constraints
506
528
}
507
529
@@ -515,21 +537,27 @@ public class TZStackView: UIView {
515
537
516
538
var topView = arrangedSubviews. first!
517
539
var bottomView = arrangedSubviews. first!
540
+
518
541
if spacerViews. count > 0 {
519
- if alignment == . Center {
542
+ switch ( alignment, axis) {
543
+ case ( . Center, _) :
520
544
topView = spacerViews [ 0 ]
521
545
bottomView = spacerViews [ 0 ]
522
- } else if alignment == . Top || alignment == . Leading {
546
+ case ( . Leading, _ ) :
523
547
bottomView = spacerViews [ 0 ]
524
- } else if alignment == . Bottom || alignment == . Trailing {
548
+ case ( . Trailing, _ ) :
525
549
topView = spacerViews [ 0 ]
526
- } else if alignment == . FirstBaseline {
527
- switch axis {
528
- case . Horizontal:
550
+ default : break
551
+ }
552
+
553
+ if #available( iOS 8 , * ) {
554
+ switch ( alignment, axis) {
555
+ case ( . FirstBaseline, . Horizontal) :
529
556
bottomView = spacerViews [ 0 ]
530
- case . Vertical:
557
+ case ( . FirstBaseline , . Vertical) :
531
558
topView = spacerViews [ 0 ]
532
559
bottomView = spacerViews [ 0 ]
560
+ default : break
533
561
}
534
562
}
535
563
}
0 commit comments