iOS11+: anchor.systemSpacing(_,multiplier:)

This commit is contained in:
relikd
2020-07-04 00:51:20 +02:00
parent 40a52d6966
commit d96994e4e9
3 changed files with 36 additions and 1 deletions

View File

@@ -6,7 +6,7 @@
Textual Auto Layout (TAL) helps you create Auto Layout constraints that you can read, understand, and maintain. Textual Auto Layout (TAL) helps you create Auto Layout constraints that you can read, understand, and maintain.
In contrast to existing libraries ([SnapKit](https://github.com/SnapKit/SnapKit), [Cartography](https://github.com/robb/Cartography)), TAL follows a minimization approach. The package consists of a single ~120 lines source file, thus TAL won't have every imaginable feature. Instead you get a dependency ideal for security audits: short and tidy. In contrast to existing libraries ([SnapKit](https://github.com/SnapKit/SnapKit), [Cartography](https://github.com/robb/Cartography)), TAL follows a minimization approach. The package consists of a single ~150 lines source file, thus TAL won't have every imaginable feature. Instead you get a dependency ideal for security audits: short and tidy.
To install, use Swift Package Manager or simply copy the source file to your project and you're good to go. To install, use Swift Package Manager or simply copy the source file to your project and you're good to go.

View File

@@ -122,3 +122,27 @@ public extension Array where Element: NSLayoutConstraint {
return self return self
} }
} }
// I couldn't find a better way to handle it, but these methods are particularly useful.
// Sadly, the syntax isn't quite as readable as the stuff above.
#if !os(macOS)
@available(iOS 11, tvOS 11, *)
public extension NSLayoutXAxisAnchor {
/// Calls and activates `.constraint(equalToSystemSpacingAfter: a)`
@discardableResult func systemSpacing(_ equal: NSLayoutXAxisAnchor, multiplier m: CGFloat = 1.0) -> NSLayoutConstraint { constraint(equalToSystemSpacingAfter: equal, multiplier: m).on() }
/// Calls and activates `.constraint(lessThanOrEqualToSystemSpacingAfter: a)`
@discardableResult func systemSpacing(lessThan a: NSLayoutXAxisAnchor, multiplier m: CGFloat = 1.0) -> NSLayoutConstraint { constraint(lessThanOrEqualToSystemSpacingAfter: a, multiplier: m).on() }
/// Calls and activates `.constraint(greaterThanOrEqualToSystemSpacingAfter: a)`
@discardableResult func systemSpacing(greaterThan a: NSLayoutXAxisAnchor, multiplier m: CGFloat = 1.0) -> NSLayoutConstraint { constraint(greaterThanOrEqualToSystemSpacingAfter: a, multiplier: m).on() }
}
@available(iOS 11, tvOS 11, *)
public extension NSLayoutYAxisAnchor {
/// Calls and activates `.constraint(equalToSystemSpacingBelow: a)`
@discardableResult func systemSpacing(_ equal: NSLayoutYAxisAnchor, multiplier m: CGFloat = 1.0) -> NSLayoutConstraint { constraint(equalToSystemSpacingBelow: equal, multiplier: m).on() }
/// Calls and activates `.constraint(lessThanOrEqualToSystemSpacingBelow: a)`
@discardableResult func systemSpacing(lessThan a: NSLayoutYAxisAnchor, multiplier m: CGFloat = 1.0) -> NSLayoutConstraint { constraint(lessThanOrEqualToSystemSpacingBelow: a, multiplier: m).on() }
/// Calls and activates `.constraint(greaterThanOrEqualToSystemSpacingBelow: a)`
@discardableResult func systemSpacing(greaterThan a: NSLayoutYAxisAnchor, multiplier m: CGFloat = 1.0) -> NSLayoutConstraint { constraint(greaterThanOrEqualToSystemSpacingBelow: a, multiplier: m).on() }
}
#endif

View File

@@ -73,4 +73,15 @@ final class TextualAutoLayoutTests: XCTestCase {
x.setActive(false) x.setActive(false)
for u in x { XCTAssertEqual(u.isActive, false) } for u in x { XCTAssertEqual(u.isActive, false) }
} }
#if !os(macOS)
@available(iOS 11, tvOS 11, *)
func testSystemSpacing() {
let x = B.topAnchor.systemSpacing(A.bottomAnchor, multiplier: 2) | .defaultLow
XCTAssertEqual(x.relation, NSLayoutConstraint.Relation.equal)
XCTAssertEqual(x.priority, ConstraintPriority.defaultLow)
XCTAssertEqual(x.constant, 16)
XCTAssertEqual(x.multiplier, 1)
}
#endif
} }