Skip to content

Commit 159d1aa

Browse files
committed
Add percent margins
1 parent 75ba647 commit 159d1aa

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

Sources/Impl/PinLayoutImpl.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,47 +818,99 @@ class PinLayoutImpl: PinLayout {
818818
return self
819819
}
820820

821+
@discardableResult
822+
func marginTop(_ percent: Percent) -> PinLayout {
823+
func context() -> String { return "marginTop(\(percent.description))" }
824+
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
825+
marginTop = percent.of(layoutSuperviewRect.height)
826+
return self
827+
}
828+
821829
@discardableResult
822830
func marginLeft(_ value: CGFloat) -> PinLayout {
823831
marginLeft = value
824832
return self
825833
}
826834

835+
@discardableResult
836+
func marginLeft(_ percent: Percent) -> PinLayout {
837+
func context() -> String { return "marginLeft(\(percent.description))" }
838+
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
839+
marginLeft = percent.of(layoutSuperviewRect.width)
840+
return self
841+
}
842+
827843
@discardableResult
828844
func marginBottom(_ value: CGFloat) -> PinLayout {
829845
marginBottom = value
830846
return self
831847
}
832848

849+
@discardableResult
850+
func marginBottom(_ percent: Percent) -> PinLayout {
851+
func context() -> String { return "marginBottom(\(percent.description))" }
852+
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
853+
marginBottom = percent.of(layoutSuperviewRect.height)
854+
return self
855+
}
856+
833857
@discardableResult
834858
func marginRight(_ value: CGFloat) -> PinLayout {
835859
marginRight = value
836860
return self
837861
}
862+
863+
@discardableResult
864+
func marginRight(_ percent: Percent) -> PinLayout {
865+
func context() -> String { return "marginRight(\(percent.description))" }
866+
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
867+
marginRight = percent.of(layoutSuperviewRect.width)
868+
return self
869+
}
838870

839871
@discardableResult
840872
func marginStart(_ value: CGFloat) -> PinLayout {
841873
return isLTR() ? marginLeft(value) : marginRight(value)
842874
}
875+
876+
@discardableResult
877+
func marginStart(_ percent: Percent) -> PinLayout {
878+
return isLTR() ? marginLeft(percent) : marginRight(percent)
879+
}
843880

844881
@discardableResult
845882
func marginEnd(_ value: CGFloat) -> PinLayout {
846883
return isLTR() ? marginRight(value) : marginLeft(value)
847884
}
848885

886+
@discardableResult
887+
func marginEnd(_ percent: Percent) -> PinLayout {
888+
return isLTR() ? marginRight(percent) : marginLeft(percent)
889+
}
890+
849891
@discardableResult
850892
func marginHorizontal(_ value: CGFloat) -> PinLayout {
851893
marginLeft = value
852894
marginRight = value
853895
return self
854896
}
855897

898+
@discardableResult
899+
func marginHorizontal(_ percent: Percent) -> PinLayout {
900+
return marginLeft(percent).marginRight(percent)
901+
}
902+
856903
@discardableResult
857904
func marginVertical(_ value: CGFloat) -> PinLayout {
858905
marginTop = value
859906
marginBottom = value
860907
return self
861908
}
909+
910+
@discardableResult
911+
func marginVertical(_ percent: Percent) -> PinLayout {
912+
return marginTop(percent).marginBottom(percent)
913+
}
862914

863915
@discardableResult
864916
func margin(_ insets: UIEdgeInsets) -> PinLayout {
@@ -888,6 +940,11 @@ class PinLayoutImpl: PinLayout {
888940
return self
889941
}
890942

943+
@discardableResult
944+
func margin(_ percent: Percent) -> PinLayout {
945+
return marginTop(percent).marginLeft(percent).marginBottom(percent).marginRight(percent)
946+
}
947+
891948
@discardableResult
892949
func margin(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> PinLayout {
893950
marginTop = top
@@ -897,6 +954,11 @@ class PinLayoutImpl: PinLayout {
897954
return self
898955
}
899956

957+
@discardableResult
958+
func margin(_ top: Percent, _ left: Percent, _ bottom: Percent, _ right: Percent) -> PinLayout {
959+
return marginTop(top).marginLeft(left).marginBottom(bottom).marginRight(right)
960+
}
961+
900962
@discardableResult func margin(_ vertical: CGFloat, _ horizontal: CGFloat) -> PinLayout {
901963
marginTop = vertical
902964
marginLeft = horizontal
@@ -905,6 +967,11 @@ class PinLayoutImpl: PinLayout {
905967
return self
906968
}
907969

970+
@discardableResult
971+
func margin(_ vertical: Percent, _ horizontal: Percent) -> PinLayout {
972+
return marginVertical(vertical).marginHorizontal(horizontal)
973+
}
974+
908975
@discardableResult func margin(_ top: CGFloat, _ horizontal: CGFloat, _ bottom: CGFloat) -> PinLayout {
909976
marginTop = top
910977
marginLeft = horizontal
@@ -913,6 +980,11 @@ class PinLayoutImpl: PinLayout {
913980
return self
914981
}
915982

983+
@discardableResult
984+
func margin(_ top: Percent, _ horizontal: Percent, _ bottom: Percent) -> PinLayout {
985+
return marginTop(top).marginHorizontal(horizontal).marginBottom(bottom)
986+
}
987+
916988
@discardableResult
917989
func pinEdges() -> PinLayout {
918990
shouldPinEdges = true

Sources/PinLayout.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,21 +404,29 @@ public protocol PinLayout {
404404
Set the top margin.
405405
*/
406406
@discardableResult func marginTop(_ value: CGFloat) -> PinLayout
407+
408+
@discardableResult func marginTop(_ percent: Percent) -> PinLayout
407409

408410
/**
409411
Set the left margin.
410412
*/
411413
@discardableResult func marginLeft(_ value: CGFloat) -> PinLayout
414+
415+
@discardableResult func marginLeft(_ percent: Percent) -> PinLayout
412416

413417
/**
414418
Set the bottom margin.
415419
*/
416420
@discardableResult func marginBottom(_ value: CGFloat) -> PinLayout
421+
422+
@discardableResult func marginBottom(_ percent: Percent) -> PinLayout
417423

418424
/**
419425
Set the right margin.
420426
*/
421427
@discardableResult func marginRight(_ value: CGFloat) -> PinLayout
428+
429+
@discardableResult func marginRight(_ percent: Percent) -> PinLayout
422430

423431
// RTL support
424432
/**
@@ -429,6 +437,8 @@ public protocol PinLayout {
429437
* In RTL direction, start margin specify the **right** margin.
430438
*/
431439
@discardableResult func marginStart(_ value: CGFloat) -> PinLayout
440+
441+
@discardableResult func marginStart(_ percent: Percent) -> PinLayout
432442

433443
/**
434444
Set the end margin.
@@ -438,17 +448,23 @@ public protocol PinLayout {
438448
* In RTL direction, end margin specify the **left** margin.
439449
*/
440450
@discardableResult func marginEnd(_ value: CGFloat) -> PinLayout
451+
452+
@discardableResult func marginEnd(_ percent: Percent) -> PinLayout
441453

442454
/**
443455
Set the left, right, start and end margins to the specified value.
444456
*/
445457
@discardableResult func marginHorizontal(_ value: CGFloat) -> PinLayout
458+
459+
@discardableResult func marginHorizontal(_ percent: Percent) -> PinLayout
446460

447461
/**
448462
Set the top and bottom margins to the specified value.
449463
*/
450464
@discardableResult func marginVertical(_ value: CGFloat) -> PinLayout
451465

466+
@discardableResult func marginVertical(_ percent: Percent) -> PinLayout
467+
452468
/**
453469
Set all margins using UIEdgeInsets.
454470
This method is particularly useful to set all margins using iOS 11 `UIView.safeAreaInsets`.
@@ -468,22 +484,30 @@ public protocol PinLayout {
468484
Set all margins to the specified value.
469485
*/
470486
@discardableResult func margin(_ value: CGFloat) -> PinLayout
487+
488+
@discardableResult func margin(_ percent: Percent) -> PinLayout
471489

472490
/**
473491
Set individually vertical margins (top, bottom) and horizontal margins (left, right, start, end).
474492
*/
475493
@discardableResult func margin(_ vertical: CGFloat, _ horizontal: CGFloat) -> PinLayout
494+
495+
@discardableResult func margin(_ vertical: Percent, _ horizontal: Percent) -> PinLayout
476496

477497
/**
478498
Set individually top, horizontal margins and bottom margin.
479499
*/
480500
@discardableResult func margin(_ top: CGFloat, _ horizontal: CGFloat, _ bottom: CGFloat) -> PinLayout
501+
502+
@discardableResult func margin(_ top: Percent, _ horizontal: Percent, _ bottom: Percent) -> PinLayout
481503

482504
/**
483505
Set individually top, left, bottom and right margins.
484506
*/
485507
@discardableResult func margin(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> PinLayout
486508

509+
@discardableResult func margin(_ top: Percent, _ left: Percent, _ bottom: Percent, _ right: Percent) -> PinLayout
510+
487511
/// Normally if only either left or right has been specified, PinLayout will MOVE the view to apply left or right margins.
488512
/// This is also true even if the width has been set.
489513
/// Calling pinEdges() will force PinLayout to pin the four edges and then apply left and/or right margins, and this without

0 commit comments

Comments
 (0)