Skip to content

Commit b82b035

Browse files
committed
added priority to the center layout builder
1 parent 1cb436c commit b82b035

6 files changed

Lines changed: 83 additions & 53 deletions

File tree

.swift-format

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"version": 1,
3+
"lineLength": 160,
4+
"indentation": {
5+
"tabs": 1
6+
},
7+
"tabWidth": 2,
8+
"maximumBlankLines": 3,
9+
"respectsExistingLineBreaks": true,
10+
"lineBreakBeforeControlFlowKeywords": false,
11+
"lineBreakBeforeEachArgument": false,
12+
"multiElementCollectionTrailingCommas": false,
13+
"indentBlankLines": false,
14+
"indentSwitchCaseLabels": true,
15+
"rules" : {
16+
}
17+
}

PinLayout/Main/Source/Layout.swift

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,23 @@ open class Layout: NSObject, NSCoding {
137137
open func pinToGuide(for view: UIView, superview: UIView, edge: Edge, gap: CGFloat) -> NSLayoutConstraint? {
138138
var constraint : NSLayoutConstraint?
139139
view.translatesAutoresizingMaskIntoConstraints = false
140-
if #available(iOS 11, *) {
141-
switch edge {
142-
case .leadingSafeArea:
143-
constraint = superview.safeAreaLayoutGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor)
144-
case .trailingSafeArea:
145-
constraint = superview.safeAreaLayoutGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor)
146-
case .topSafeArea:
147-
constraint = superview.safeAreaLayoutGuide.topAnchor.constraint(equalTo: view.topAnchor)
148-
case .bottomSafeArea:
149-
constraint = superview.safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: view.bottomAnchor)
150-
default:
151-
break
152-
}
140+
switch edge {
141+
case .leadingSafeArea:
142+
constraint = superview.safeAreaLayoutGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor)
143+
case .trailingSafeArea:
144+
constraint = superview.safeAreaLayoutGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor)
145+
case .topSafeArea:
146+
constraint = superview.safeAreaLayoutGuide.topAnchor.constraint(equalTo: view.topAnchor)
147+
case .bottomSafeArea:
148+
constraint = superview.safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: view.bottomAnchor)
149+
case .leadingReadable:
150+
constraint = superview.readableContentGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor)
151+
case .trailingReadable:
152+
constraint = superview.readableContentGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor)
153+
default:
154+
break
153155
}
154156

155-
if #available(iOS 9, *) {
156-
switch edge {
157-
case .leadingReadable:
158-
constraint = superview.readableContentGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor)
159-
case .trailingReadable:
160-
constraint = superview.readableContentGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor)
161-
default:
162-
break
163-
}
164-
}
165157

166158
if let constraint = constraint {
167159
switch edge {

PinLayout/Main/Source/LayoutBuilder.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ public class LayoutBuilder {
3737
}
3838

3939
@discardableResult
40-
public func pin(_ edges: Layout.Edge..., insets: NSDirectionalEdgeInsets = .zero, gap: CGFloat = 0, priority: UILayoutPriority = .required, relatedBy: NSLayoutConstraint.Relation = .equal) -> LayoutBuilder {
40+
public func pin(
41+
_ edges: Layout.Edge..., insets: NSDirectionalEdgeInsets = .zero, gap: CGFloat = 0, priority: UILayoutPriority = .required,
42+
relatedBy: NSLayoutConstraint.Relation = .equal
43+
) -> LayoutBuilder {
4144
for edge in edges {
42-
let gapValue = insets.value(edge: edge) ?? gap
45+
let gapValue = insets.value(edge: edge) ?? gap
4346
if let constraint = layout.pin(view: view, to: edge, gap: gapValue, relatedBy: relatedBy) {
4447
constraint.priority = priority
4548
}
@@ -52,9 +55,11 @@ public class LayoutBuilder {
5255
}
5356

5457
@discardableResult
55-
public func pin(_ edges: Layout.Edge..., insets: NSDirectionalEdgeInsets = .zero, gap: Layout.Defaults, priority: UILayoutPriority = .required) -> LayoutBuilder {
58+
public func pin(_ edges: Layout.Edge..., insets: NSDirectionalEdgeInsets = .zero, gap: Layout.Defaults, priority: UILayoutPriority = .required)
59+
-> LayoutBuilder
60+
{
5661
for edge in edges {
57-
let gapValue = insets.value(edge: edge) ?? layout.value(for: gap)
62+
let gapValue = insets.value(edge: edge) ?? layout.value(for: gap)
5863
if let constraint = layout.pin(view: view, to: edge, gap: gapValue) {
5964
constraint.priority = priority
6065
}
@@ -64,7 +69,7 @@ public class LayoutBuilder {
6469

6570

6671
@discardableResult
67-
public func pin(_ edge: Layout.Edge, to view: UIView, gap: Layout.Defaults, priority: UILayoutPriority = .required) -> LayoutBuilder {
72+
public func pin(_ edge: Layout.Edge, to view: UIView, gap: Layout.Defaults, priority: UILayoutPriority = .required) -> LayoutBuilder {
6873
let gapValue = layout.value(for: gap)
6974
return self.pin(edge, to: view, gap: gapValue, priority: priority)
7075
}
@@ -77,8 +82,8 @@ public class LayoutBuilder {
7782
}
7883

7984
@discardableResult
80-
public func centerX(offset: CGFloat = 0) -> LayoutBuilder {
81-
layout.centerX(view: view, offset: offset)
85+
public func centerX(offset: CGFloat = 0, priority: UILayoutPriority = .required) -> LayoutBuilder {
86+
layout.centerX(view: view, offset: offset, priority: priority)
8287
return self
8388
}
8489

@@ -90,8 +95,8 @@ public class LayoutBuilder {
9095
}
9196

9297
@discardableResult
93-
public func centerY(offset: CGFloat = 0) -> LayoutBuilder {
94-
layout.centerY(view: view, offset: offset)
98+
public func centerY(offset: CGFloat = 0, priority: UILayoutPriority = .required) -> LayoutBuilder {
99+
layout.centerY(view: view, offset: offset, priority: priority)
95100
return self
96101
}
97102

@@ -188,4 +193,3 @@ public class LayoutBuilder {
188193
return self
189194
}
190195
}
191-

PinLayout/Main/Source/Layout_Center.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ extension Layout {
3838

3939
@objc
4040
@discardableResult
41-
open func equalCenterX(view: UIView, toView secondView: UIView, offset: CGFloat = 0) -> NSLayoutConstraint? {
42-
self.setEqualConstant(view: view, andView: secondView, attribute: .centerX, constant: offset)
41+
open func equalCenterX(view: UIView, toView secondView: UIView, offset: CGFloat = 0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? {
42+
self.setEqualConstant(view: view, andView: secondView, attribute: .centerX, priority: priority, multiplier: 1, constant: offset)
4343
}
4444

4545

PinLayout/Main/Source/Layout_SafeCenter.swift

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,16 @@ extension Layout {
2626
}
2727

2828
@discardableResult
29-
public func centerX(view: UIView, with other: UIView? = nil, offset: CGFloat = 0) -> NSLayoutConstraint? {
29+
public func centerX(view: UIView, with other: UIView? = nil, offset: CGFloat = 0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? {
3030
guard let otherView = self.otherView(view: view, other: other) else {
3131
return nil
3232
}
33-
if #available(iOS 11, *) {
34-
let constraint = view.safeAreaLayoutGuide.centerXAnchor.constraint(equalTo: otherView.safeAreaLayoutGuide.centerXAnchor)
35-
constraint.isActive = true
36-
constraint.constant = offset
37-
recorder?.append(constraint)
38-
return constraint
39-
}
40-
return self.equalCenterX(view: view)
33+
let constraint = view.safeAreaLayoutGuide.centerXAnchor.constraint(equalTo: otherView.safeAreaLayoutGuide.centerXAnchor)
34+
constraint.isActive = true
35+
constraint.constant = offset
36+
constraint.priority = priority
37+
recorder?.append(constraint)
38+
return constraint
4139
}
4240

4341
@discardableResult
@@ -47,18 +45,16 @@ extension Layout {
4745
}
4846

4947
@discardableResult
50-
public func centerY(view: UIView, with other: UIView? = nil, offset: CGFloat = 0) -> NSLayoutConstraint? {
48+
public func centerY(view: UIView, with other: UIView? = nil, offset: CGFloat = 0, priority: UILayoutPriority = .required) -> NSLayoutConstraint? {
5149
guard let otherView = self.otherView(view: view, other: other) else {
5250
return nil
5351
}
54-
if #available(iOS 11, *) {
55-
let constraint = view.safeAreaLayoutGuide.centerYAnchor.constraint(equalTo: otherView.safeAreaLayoutGuide.centerYAnchor)
56-
constraint.isActive = true
57-
constraint.constant = offset
58-
recorder?.append(constraint)
59-
return constraint
60-
}
61-
return self.equalCenterY(view: view)
52+
let constraint = view.safeAreaLayoutGuide.centerYAnchor.constraint(equalTo: otherView.safeAreaLayoutGuide.centerYAnchor)
53+
constraint.isActive = true
54+
constraint.constant = offset
55+
constraint.priority = priority
56+
recorder?.append(constraint)
57+
return constraint
6258
}
6359

6460
@objc(centerView:)

PinLayout/Test/Source/LayoutBuilder_Test.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,17 @@ class LayoutBuilder_Test: PinLayout_Base_Test {
243243
assertThat(constraint?.constant, presentAnd(equalTo(20)))
244244
}
245245

246+
func test_view_is_horizontal_center_with_priority() {
247+
let superview = UIView()
248+
superview.addSubview(view)
249+
250+
let constraint = view.layout.centerX(priority: .defaultLow).constraints.first
251+
252+
// then
253+
assertThat(constraint, present())
254+
assertThat(constraint?.priority, presentAnd(equalTo(.defaultLow)))
255+
}
256+
246257
func test_view_is_horizontal_center_with_other_view() {
247258
let superview = UIView()
248259
let other = UIView()
@@ -293,6 +304,16 @@ class LayoutBuilder_Test: PinLayout_Base_Test {
293304
assertThat(constraint?.constant, presentAnd(equalTo(20)))
294305
}
295306

307+
func test_view_is_vertical_center_with_priority() {
308+
let superview = UIView()
309+
superview.addSubview(view)
310+
311+
let constraint = view.layout.centerY(priority: .defaultLow).constraints.first
312+
313+
// then
314+
assertThat(constraint, present())
315+
assertThat(constraint?.priority, presentAnd(equalTo(.defaultLow)))
316+
}
296317

297318
func test_view_is_vertical_center_with_other_view() {
298319
let superview = UIView()

0 commit comments

Comments
 (0)