@@ -40,54 +40,21 @@ public extension UIView {
40
40
paddingLeft: CGFloat = 0 ,
41
41
paddingBottom: CGFloat = 0 ,
42
42
paddingRight: CGFloat = 0 ,
43
- width: CGFloat = 0 ,
44
- height: CGFloat = 0 ) {
43
+ width: CGFloat ? = nil ,
44
+ height: CGFloat ? = nil ) {
45
45
46
46
translatesAutoresizingMaskIntoConstraints = false
47
47
48
- if let centerX = centerX {
49
- let centerX = centerXAnchor. constraint ( equalTo: centerX, constant: paddingCenterX)
50
- centerX. isActive = true
51
- }
52
-
53
- if let centerY = centerY {
54
- let centerY = centerYAnchor. constraint ( equalTo: centerY, constant: paddingCenterY)
55
- centerY. isActive = true
56
- }
57
-
58
- if let top = top {
59
- let top = topAnchor. constraint ( equalTo: top, constant: paddingTop)
60
- top. priority = UILayoutPriority ( rawValue: 750 )
61
- top. isActive = true
62
- }
63
-
64
- if let left = left {
65
- let left = leftAnchor. constraint ( equalTo: left, constant: paddingLeft)
66
- left. priority = UILayoutPriority ( rawValue: 999 )
67
- left. isActive = true
68
- }
69
-
70
- if let bottom = bottom {
71
- let bottom = bottomAnchor. constraint ( equalTo: bottom, constant: paddingBottom)
72
- bottom. priority = UILayoutPriority ( rawValue: 750 )
73
- bottom. isActive = true
74
- }
75
-
76
- if let right = right {
77
- let right = rightAnchor. constraint ( equalTo: right, constant: paddingRight)
78
- right. priority = UILayoutPriority ( rawValue: 999 )
79
- right. isActive = true
80
- }
81
-
82
- if width != 0 {
83
- let width = widthAnchor. constraint ( equalToConstant: width)
84
- width. isActive = true
85
- }
86
-
87
- if height != 0 {
88
- let height = heightAnchor. constraint ( equalToConstant: height)
89
- height. isActive = true
90
- }
48
+ NSLayoutConstraint . activate ( [
49
+ centerX? . constraint ( equalTo: centerXAnchor, constant: - paddingCenterX) ,
50
+ centerY? . constraint ( equalTo: centerYAnchor, constant: - paddingCenterY) ,
51
+ top? . constraint ( equalTo: topAnchor, constant: - paddingTop, priority: 750 ) ,
52
+ left? . constraint ( equalTo: leftAnchor, constant: - paddingLeft, priority: 999 ) ,
53
+ bottom? . constraint ( equalTo: bottomAnchor, constant: - paddingBottom, priority: 750 ) ,
54
+ right? . constraint ( equalTo: rightAnchor, constant: - paddingRight, priority: 999 ) ,
55
+ widthAnchor. constraint ( equalToOptionalConstant: width) ,
56
+ heightAnchor. constraint ( equalToOptionalConstant: height)
57
+ ] . compactMap { $0 } )
91
58
}
92
59
93
60
/**
@@ -105,29 +72,12 @@ public extension UIView {
105
72
106
73
translatesAutoresizingMaskIntoConstraints = false
107
74
108
- if let superviewTopAnchor = superview? . topAnchor {
109
- let top = topAnchor. constraint ( equalTo: superviewTopAnchor, constant: paddingTop)
110
- top. priority = UILayoutPriority ( rawValue: 750 )
111
- top. isActive = true
112
- }
113
-
114
- if let superviewLeadingAnchor = superview? . leadingAnchor {
115
- let leading = leadingAnchor. constraint ( equalTo: superviewLeadingAnchor, constant: paddingLeft)
116
- leading. priority = UILayoutPriority ( rawValue: 999 )
117
- leading. isActive = true
118
- }
119
-
120
- if let superviewBottomAnchor = superview? . bottomAnchor {
121
- let bottom = bottomAnchor. constraint ( equalTo: superviewBottomAnchor, constant: paddingBottom)
122
- bottom. priority = UILayoutPriority ( rawValue: 750 )
123
- bottom. isActive = true
124
- }
125
-
126
- if let superviewTrailingAnchor = superview? . trailingAnchor {
127
- let trailing = trailingAnchor. constraint ( equalTo: superviewTrailingAnchor, constant: paddingRight)
128
- trailing. priority = UILayoutPriority ( rawValue: 999 )
129
- trailing. isActive = true
130
- }
75
+ NSLayoutConstraint . activate ( [
76
+ superview? . topAnchor. constraint ( equalTo: topAnchor, constant: - paddingTop, priority: 750 ) ,
77
+ superview? . leadingAnchor. constraint ( equalTo: leadingAnchor, constant: - paddingLeft, priority: 999 ) ,
78
+ superview? . bottomAnchor. constraint ( equalTo: bottomAnchor, constant: - paddingBottom, priority: 750 ) ,
79
+ superview? . trailingAnchor. constraint ( equalTo: trailingAnchor, constant: - paddingRight, priority: 999 )
80
+ ] . compactMap { $0 } )
131
81
}
132
82
133
83
/**
0 commit comments