Skip to content

Commit b3b5431

Browse files
author
Alan Westbrook
committed
Anchor extensions.
* Anchor extensions for multiplicative versions of Axis
1 parent 7e23fcd commit b3b5431

File tree

6 files changed

+48
-48
lines changed

6 files changed

+48
-48
lines changed

Onboarding.xcodeproj/project.pbxproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
isa = PBXProject;
100100
attributes = {
101101
LastSwiftUpdateCheck = 0730;
102-
LastUpgradeCheck = 0730;
102+
LastUpgradeCheck = 0800;
103103
ORGANIZATIONNAME = rockwood;
104104
TargetAttributes = {
105105
E755CDA91CAF236900980DE8 = {
@@ -279,6 +279,7 @@
279279
PRODUCT_BUNDLE_IDENTIFIER = com.rockwood.Onboarding;
280280
PRODUCT_NAME = "$(TARGET_NAME)";
281281
SKIP_INSTALL = YES;
282+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
282283
SWIFT_VERSION = 3.0;
283284
};
284285
name = Release;

Onboarding.xcodeproj/xcshareddata/xcschemes/Onboarding.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 = "0730"
3+
LastUpgradeVersion = "0800"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Sources/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.1</string>
18+
<string>1.2</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

Sources/NSLayoutConstraint+Convenience.swift

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,46 @@ import Foundation
1010
import UIKit
1111

1212
extension NSLayoutConstraint {
13-
// Apple hasn't bothered to make multiplicitave constraints for anchors that aren't dimensions...
14-
public class func constraintFor(view:UIView, attribute:NSLayoutAttribute, equalToView:UIView, multiplier:CGFloat) -> NSLayoutConstraint {
15-
return NSLayoutConstraint(item: view,
16-
attribute: attribute,
17-
relatedBy: .equal,
18-
toItem: equalToView,
19-
attribute: attribute,
20-
multiplier: multiplier,
21-
constant: 0)
13+
14+
public class func constraints(for view:UIView, filling:UIView) -> [NSLayoutConstraint] {
15+
return [view.leadingAnchor.constraint(equalTo: filling.leadingAnchor),
16+
view.trailingAnchor.constraint(equalTo: filling.trailingAnchor),
17+
view.topAnchor.constraint(equalTo: filling.topAnchor),
18+
view.bottomAnchor.constraint(equalTo: filling.bottomAnchor)]
2219
}
20+
}
21+
22+
// Apple hasn't bothered to make multiplicitave constraints for anchors that aren't dimensions...
23+
// This weird function signature is working around a compiler bug.
24+
private func con(straint constraint:NSLayoutConstraint, multiplier:CGFloat) -> NSLayoutConstraint {
25+
return NSLayoutConstraint(item: constraint.firstItem, attribute: constraint.firstAttribute, relatedBy: constraint.relation, toItem: constraint.secondItem, attribute: constraint.secondAttribute, multiplier: multiplier, constant: constraint.constant)
26+
}
2327

24-
public class func constraintFor(view:UIView, attribute:NSLayoutAttribute, lessThanOrEqualToView:UIView, multiplier:CGFloat) -> NSLayoutConstraint {
25-
return NSLayoutConstraint(item: view,
26-
attribute: attribute,
27-
relatedBy: .lessThanOrEqual,
28-
toItem: lessThanOrEqualToView,
29-
attribute: attribute,
30-
multiplier: multiplier,
31-
constant: 0)
28+
extension NSLayoutYAxisAnchor {
29+
func constraint(equalTo anchor: NSLayoutAnchor<NSLayoutYAxisAnchor>, multiplier:CGFloat) -> NSLayoutConstraint {
30+
return con(straint: super.constraint(equalTo: anchor), multiplier: multiplier)
3231
}
3332

34-
public class func constraintFor(view:UIView, attribute:NSLayoutAttribute, greaterThanOrEqualToView:UIView, multiplier:CGFloat) -> NSLayoutConstraint {
35-
return NSLayoutConstraint(item: view,
36-
attribute: attribute,
37-
relatedBy: .greaterThanOrEqual,
38-
toItem: greaterThanOrEqualToView,
39-
attribute: attribute,
40-
multiplier: multiplier,
41-
constant: 0)
33+
func constraint(lessThanOrEqualTo anchor: NSLayoutAnchor<NSLayoutYAxisAnchor>, multiplier:CGFloat) -> NSLayoutConstraint {
34+
return con(straint: super.constraint(lessThanOrEqualTo: anchor), multiplier: multiplier)
35+
}
36+
37+
func constraint(greaterThanOrEqualTo anchor: NSLayoutAnchor<NSLayoutYAxisAnchor>, multiplier:CGFloat) -> NSLayoutConstraint {
38+
return con(straint: super.constraint(greaterThanOrEqualTo: anchor), multiplier: multiplier)
4239
}
40+
}
4341

44-
public class func constraintsFor(view:UIView, fillingParentView:UIView) -> [NSLayoutConstraint] {
45-
return [view.leadingAnchor.constraint(equalTo: fillingParentView.leadingAnchor),
46-
view.trailingAnchor.constraint(equalTo: fillingParentView.trailingAnchor),
47-
view.topAnchor.constraint(equalTo: fillingParentView.topAnchor),
48-
view.bottomAnchor.constraint(equalTo: fillingParentView.bottomAnchor)]
42+
extension NSLayoutXAxisAnchor {
43+
func constraint(equalTo anchor: NSLayoutAnchor<NSLayoutXAxisAnchor>, multiplier:CGFloat) -> NSLayoutConstraint {
44+
return con(straint: super.constraint(equalTo: anchor), multiplier: multiplier)
45+
}
46+
47+
func constraint(lessThanOrEqualTo anchor: NSLayoutAnchor<NSLayoutXAxisAnchor>, multiplier:CGFloat) -> NSLayoutConstraint {
48+
return con(straint: super.constraint(lessThanOrEqualTo: anchor), multiplier: multiplier)
49+
}
50+
51+
func constraint(greaterThanOrEqualTo anchor: NSLayoutAnchor<NSLayoutXAxisAnchor>, multiplier:CGFloat) -> NSLayoutConstraint {
52+
return con(straint: super.constraint(greaterThanOrEqualTo: anchor), multiplier: multiplier)
4953
}
5054
}
55+

Sources/OnboardingPage.swift

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import UIKit
1010

1111
public
12-
protocol OnboardingDoneDelegate {
12+
protocol OnboardingDoneDelegate: AnyObject {
1313
func donePressed(_ page:OnboardingPage)
1414
}
1515

@@ -120,7 +120,7 @@ class OnboardingContentPage : OnboardingPage {
120120
backgroundImageView.contentMode = .scaleAspectFill
121121

122122
insertSubview(backgroundImageView, at: 0)
123-
NSLayoutConstraint.activate((NSLayoutConstraint.constraintsFor(view: backgroundImageView, fillingParentView: self)))
123+
NSLayoutConstraint.activate((NSLayoutConstraint.constraints(for: backgroundImageView, filling: self)))
124124
}
125125

126126
private func setupTitleTopStyle() {
@@ -148,20 +148,14 @@ class OnboardingContentPage : OnboardingPage {
148148
}
149149

150150
private func setupTitleSubordinateStyle() {
151-
152-
NSLayoutConstraint(
153-
item: foregroundImageView,
154-
attribute: .centerY,
155-
relatedBy: .equal,
156-
toItem: self,
157-
attribute: .centerY,
158-
multiplier: 0.66,
159-
constant: 0).isActive = true
160151

161152
titleLabel.font = UIFont.systemFont(ofSize: 24)
162153
titleLabel.numberOfLines = 0
163-
NSLayoutConstraint.constraintFor(view: titleLabel, attribute: .centerY, equalToView: self, multiplier: 1.25).isActive = true
164154

155+
NSLayoutConstraint.activate([
156+
titleLabel.centerYAnchor.constraint(equalTo: centerYAnchor, multiplier: 1.25),
157+
foregroundImageView.centerYAnchor.constraint(equalTo: centerYAnchor, multiplier: 0.66)
158+
])
165159

166160
setupCommonTitleConstraints()
167161
setupCommonForegroundImageConstraints()
@@ -231,7 +225,7 @@ class OnboardingFinalPage : OnboardingContentPage {
231225
override public func setupConstraints() {
232226
super.setupConstraints()
233227

234-
let buttonYConstraint = NSLayoutConstraint.constraintFor(view: doneButton, attribute: .centerY, equalToView: self, multiplier: 1.7)
228+
let buttonYConstraint = doneButton.centerYAnchor.constraint(equalTo: centerYAnchor, multiplier: 1.7)
235229
buttonYConstraint.priority = UILayoutPriorityDefaultLow
236230

237231
NSLayoutConstraint.activate([

Sources/OnboardingViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class OnboardingViewController: UIViewController, UIScrollViewDelegate, Onboardi
215215
pager.pageIndicatorTintColor = UIColor.lightGray()
216216
overlayView.addSubview(pager)
217217

218-
NSLayoutConstraint.activate(NSLayoutConstraint.constraintsFor(view: overlayView, fillingParentView: view))
218+
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(for: overlayView, filling: view))
219219

220220
NSLayoutConstraint.activate([
221221
pager.leadingAnchor.constraint(equalTo: overlayView.leadingAnchor),
@@ -231,7 +231,7 @@ class OnboardingViewController: UIViewController, UIScrollViewDelegate, Onboardi
231231
scroller.showsHorizontalScrollIndicator = false
232232
view.addSubview(scroller)
233233

234-
NSLayoutConstraint.activate(NSLayoutConstraint.constraintsFor(view: scroller, fillingParentView: view))
234+
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(for: scroller, filling: view))
235235
}
236236

237237
public func scrollViewDidScroll(_ scrollView: UIScrollView) {

0 commit comments

Comments
 (0)