Skip to content

Commit 1316819

Browse files
authored
Merge pull request #10 from Weebly/width-and-height-methods
Easy Width & Height Constraints
2 parents 1208c91 + 8dba31a commit 1316819

File tree

7 files changed

+80
-42
lines changed

7 files changed

+80
-42
lines changed

AnchorKit.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "AnchorKit"
4-
s.version = "2.1.0"
4+
s.version = "2.1.1"
55
s.summary = "A Swifty anchor-based API for AutoLayout."
66

77
s.description = <<-DESC
@@ -20,7 +20,7 @@ Pod::Spec.new do |s|
2020
s.osx.deployment_target = "10.11"
2121
s.tvos.deployment_target = "9.0"
2222

23-
s.source = { :git => "https://github.com/Weebly/AnchorKit.git", :tag => "v2.1.0" }
23+
s.source = { :git => "https://github.com/Weebly/AnchorKit.git", :tag => "v2.1.1" }
2424
s.requires_arc = true
2525
s.source_files = "AnchorKit/Source/*.swift"
2626

AnchorKit.xcodeproj/project.pbxproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@
238238
isa = PBXProject;
239239
attributes = {
240240
LastSwiftUpdateCheck = 0820;
241-
LastUpgradeCheck = 0900;
241+
LastUpgradeCheck = 0940;
242242
ORGANIZATIONNAME = "Eddie Kaiger";
243243
TargetAttributes = {
244244
C01EC55F1E8F002500BEBB0F = {
@@ -496,13 +496,15 @@
496496
CLANG_WARN_BOOL_CONVERSION = YES;
497497
CLANG_WARN_COMMA = YES;
498498
CLANG_WARN_CONSTANT_CONVERSION = YES;
499+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
499500
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
500501
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
501502
CLANG_WARN_EMPTY_BODY = YES;
502503
CLANG_WARN_ENUM_CONVERSION = YES;
503504
CLANG_WARN_INFINITE_RECURSION = YES;
504505
CLANG_WARN_INT_CONVERSION = YES;
505506
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
507+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
506508
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
507509
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
508510
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
@@ -559,13 +561,15 @@
559561
CLANG_WARN_BOOL_CONVERSION = YES;
560562
CLANG_WARN_COMMA = YES;
561563
CLANG_WARN_CONSTANT_CONVERSION = YES;
564+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
562565
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
563566
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
564567
CLANG_WARN_EMPTY_BODY = YES;
565568
CLANG_WARN_ENUM_CONVERSION = YES;
566569
CLANG_WARN_INFINITE_RECURSION = YES;
567570
CLANG_WARN_INT_CONVERSION = YES;
568571
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
572+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
569573
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
570574
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
571575
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;

AnchorKit.xcodeproj/xcshareddata/xcschemes/AnchorKit.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0900"
3+
LastUpgradeVersion = "0940"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

AnchorKit.xcodeproj/xcshareddata/xcschemes/AnchorKitTests.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0900"
3+
LastUpgradeVersion = "0940"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

AnchorKit/Source/Anchorable+Constraints.swift

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -118,78 +118,69 @@ extension Anchorable {
118118

119119
/**
120120
Constrains the edges of the current item to another item by creating and activating the leading, trailing, top, and bottom constraints.
121-
- parameter relation: The relation for all of the constraints. If you want to use `.equal`, you can use `constrainEdges(to:priority:)` instead.
121+
- parameter relation: The relation for all of the constraints. Default is `.equal`.
122122
- parameter item: The item to which to constrain.
123123
- parameter priority: The layout priority to set for the constraints. Default is `.required`.
124124
- returns: The newly created and activated constraints for the leading, trailing, top, and bottom anchors.
125125
*/
126126
@discardableResult
127-
public func constrainEdges<AnchorableType: Anchorable>(_ relation: Relation, to item: AnchorableType, priority: LayoutPriority = .required) -> [NSLayoutConstraint] {
127+
public func constrainEdges<AnchorableType: Anchorable>(_ relation: Relation = .equal, to item: AnchorableType, priority: LayoutPriority = .required) -> [NSLayoutConstraint] {
128128
return constrain(.leading, .trailing, .top, .bottom, relation: relation, to: item, priority: priority)
129129
}
130130

131-
/**
132-
Constrains the edges of the current item to another item by creating and activating the leading, trailing, top, and bottom constraints. If you want to use this with a relation, use `constrainEdges(_:to:priority:)`.
133-
- parameter item: The item to which to constrain.
134-
- parameter priority: The layout priority to set for the constraints. Default is `.required`.
135-
- returns: The newly created and activated constraints for the leading, trailing, top, and bottom anchors.
136-
*/
137-
@discardableResult
138-
public func constrainEdges<AnchorableType: Anchorable>(to item: AnchorableType, priority: LayoutPriority = .required) -> [NSLayoutConstraint] {
139-
return constrainEdges(.equal, to: item, priority: priority)
140-
}
141-
142131
// MARK: - Center Constraints
143132

144-
/**
145-
Convenience method for centering the current item in another item by creating the centerX and centerY constraints. If you want to use this with a relation, use `constrainCenter(_:to:priority:)`.
146-
- parameter item: The item in which to center the current item.
147-
- parameter priority: The layout priority to set for the constraints. Default is `.required`.
148-
- returns: The newly created and activated constraints for the centerX and centerY anchors.
149-
*/
150-
@discardableResult
151-
public func constrainCenter<AnchorableType: Anchorable>(to item: AnchorableType, priority: LayoutPriority = .required) -> [NSLayoutConstraint] {
152-
return constrainCenter(.equal, to: item, priority: priority)
153-
}
154-
155133
/**
156134
Convenience method for centering the current item in another item by creating the centerX and centerY constraints.
157-
- parameter relation: The relation for both constraints. If you want to use `.equal`, you can also just use `constrainCenter(to:priority:)` instead.
135+
- parameter relation: The relation for both constraints. Default is `.equal`.
158136
- parameter item: The item in which to center the current item.
159137
- parameter priority: The layout priority to set for the constraints. Default is `.required`.
160138
- returns: The newly created and activated constraints for the centerX and centerY anchors.
161139
*/
162140
@discardableResult
163-
public func constrainCenter<AnchorableType: Anchorable>(_ relation: Relation, to item: AnchorableType, priority: LayoutPriority = .required) -> [NSLayoutConstraint] {
141+
public func constrainCenter<AnchorableType: Anchorable>(_ relation: Relation = .equal, to item: AnchorableType, priority: LayoutPriority = .required) -> [NSLayoutConstraint] {
164142
return constrain(.centerX, .centerY, relation: relation, to: item, priority: priority)
165143
}
166144

167145
// MARK: - Size Constraints
168146

169147
/**
170148
Constrains the width and height of the current item to a specific size.
149+
- parameter relation: The relation for all of the constraints. Default is `.equal`.
171150
- parameter size: The size to which to constrain.
172151
- parameter priority: The layout priority to set for the constraints. Default is `.required`.
173152
- returns: The newly created and activated constraints for the width and height anchors.
174153
*/
175154
@discardableResult
176-
public func constrain(to size: CGSize, priority: LayoutPriority = .required) -> [NSLayoutConstraint] {
177-
return constrain(.equal, to: size, priority: priority)
155+
public func constrain(_ relation: Relation = .equal, to size: CGSize, priority: LayoutPriority = .required) -> [NSLayoutConstraint] {
156+
return [
157+
constrain(.width, relation: relation, toConstant: size.width, priority: priority),
158+
constrain(.height, relation: relation, toConstant: size.height, priority: priority)
159+
]
178160
}
179161

180162
/**
181-
Constrains the width and height of the current item to a specific size.
182-
- parameter relation: The relation for all of the constraints. If you want to use `.equal`, you can use `constrain(to:priority:)` instead.
183-
- parameter size: The size to which to constrain.
163+
Constrains the height of the current item to a specific value.
164+
- parameter relation: The relation for the constraint. Default is `.equal`.
165+
- parameter height: The height to which to constrain.
184166
- parameter priority: The layout priority to set for the constraints. Default is `.required`.
185-
- returns: The newly created and activated constraints for the width and height anchors.
167+
- returns: The newly created and activated constraint for height anchor.
186168
*/
187169
@discardableResult
188-
public func constrain(_ relation: Relation, to size: CGSize, priority: LayoutPriority = .required) -> [NSLayoutConstraint] {
189-
return [
190-
constrain(.width, relation: relation, toConstant: size.width, priority: priority),
191-
constrain(.height, relation: relation, toConstant: size.height, priority: priority)
192-
]
170+
public func constrainHeight(_ relation: Relation = .equal, to height: CGFloatRepresentable, priority: LayoutPriority = .required) -> NSLayoutConstraint {
171+
return constrain(.height, relation: relation, toConstant: height, priority: priority)
172+
}
173+
174+
/**
175+
Constrains the width of the current item to a specific value.
176+
- parameter relation: The relation for the constraint. Default is `.equal`.
177+
- parameter width: The width to which to constrain.
178+
- parameter priority: The layout priority to set for the constraints. Default is `.required`.
179+
- returns: The newly created and activated constraint for width anchor.
180+
*/
181+
@discardableResult
182+
public func constrainWidth(_ relation: Relation = .equal, to width: CGFloatRepresentable, priority: LayoutPriority = .required) -> NSLayoutConstraint {
183+
return constrain(.width, relation: relation, toConstant: width, priority: priority)
193184
}
194185

195186
#if os(iOS) || os(tvOS)

AnchorKitTests/Anchorable_Tests.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,10 @@ class Anchorable_Tests: XCTestCase {
348348
func testConstrainToSize() {
349349
let constraints = view1.constrain(to: CGSize(width: 20, height: 30), priority: .medium)
350350
XCTAssertEqual(constraints[0].firstAnchor, view1.widthAnchor)
351+
XCTAssertNil(constraints[0].secondAnchor)
351352
XCTAssertEqual(constraints[0].constant, 20)
352353
XCTAssertEqual(constraints[1].firstAnchor, view1.heightAnchor)
354+
XCTAssertNil(constraints[1].secondAnchor)
353355
XCTAssertEqual(constraints[1].constant, 30)
354356
}
355357

@@ -376,6 +378,42 @@ class Anchorable_Tests: XCTestCase {
376378

377379
// MARK: - Width and Height
378380

381+
func testConstrainHeight() {
382+
let constraint = view1.constrainHeight(to: 20)
383+
XCTAssertEqual(constraint.firstAnchor, view1.heightAnchor)
384+
XCTAssertNil(constraint.secondAnchor)
385+
XCTAssertEqual(constraint.constant, 20)
386+
}
387+
388+
func testConstrainHeight_withOptions() {
389+
let constraint = view1.constrainHeight(.greaterThanOrEqual, to: 12, priority: .low)
390+
XCTAssertEqual(constraint.relation, .greaterThanOrEqual)
391+
XCTAssertEqual(constraint.layoutPriority, .low)
392+
}
393+
394+
func testConstrainToHeight_defaults() {
395+
let constraint = view1.constrainHeight(to: 20)
396+
testDefaults(for: constraint, constant: 20)
397+
}
398+
399+
func testConstrainWidth() {
400+
let constraint = view1.constrainWidth(to: 20)
401+
XCTAssertEqual(constraint.firstAnchor, view1.widthAnchor)
402+
XCTAssertNil(constraint.secondAnchor)
403+
XCTAssertEqual(constraint.constant, 20)
404+
}
405+
406+
func testConstrainWidth_withOptions() {
407+
let constraint = view1.constrainWidth(.greaterThanOrEqual, to: 12, priority: .medium)
408+
XCTAssertEqual(constraint.relation, .greaterThanOrEqual)
409+
XCTAssertEqual(constraint.layoutPriority, .medium)
410+
}
411+
412+
func testConstrainToWidth_defaults() {
413+
let constraint = view1.constrainWidth(to: 20)
414+
testDefaults(for: constraint, constant: 20)
415+
}
416+
379417
func testUpdateWidth() {
380418
let constraints = view1.constrain(.width, .height, toConstant: 100)
381419
view1.updateWidth(200)

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ myView.constrainEdges(to: anotherView).insetVertical(20).insetHorizontal(30)
2323

2424
// Set height/width equal to a constant
2525
myView.constrain(.height, .width, toConstant: 200)
26+
myView.constrainWidth(to: 42)
2627

2728
// Set the height/width equal to a CGSize
2829
myView.constrain(to: CGSize(width: 100, height: 200))
@@ -159,6 +160,10 @@ Constrain an anchor of an item to a constant. This is is especially useful (and
159160
````swift
160161
myView.constrain(.height, toConstant: 200)
161162
myBoxView.constrain(.width, .height, toConstant: 50) // Creates a box
163+
164+
// Even easier:
165+
myView.constrainHeight(to: 42)
166+
myView.constrainWidth(to: 60)
162167
````
163168

164169
Calling this method with a single anchor will implicitly return `NSLayoutConstraint`. Otherwise, the return type is `[NSLayoutConstraint]`. The full signatures of these methods are shown below.

0 commit comments

Comments
 (0)