Skip to content

Commit

Permalink
Anchor extensions.
Browse files Browse the repository at this point in the history
 * Anchor extensions for multiplicative versions of Axis
  • Loading branch information
Alan Westbrook committed Jun 21, 2016
1 parent 7e23fcd commit b3b5431
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 48 deletions.
3 changes: 2 additions & 1 deletion Onboarding.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0730;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = rockwood;
TargetAttributes = {
E755CDA91CAF236900980DE8 = {
Expand Down Expand Up @@ -279,6 +279,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.rockwood.Onboarding;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion Sources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.1</string>
<string>1.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
65 changes: 35 additions & 30 deletions Sources/NSLayoutConstraint+Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,46 @@ import Foundation
import UIKit

extension NSLayoutConstraint {
// Apple hasn't bothered to make multiplicitave constraints for anchors that aren't dimensions...
public class func constraintFor(view:UIView, attribute:NSLayoutAttribute, equalToView:UIView, multiplier:CGFloat) -> NSLayoutConstraint {
return NSLayoutConstraint(item: view,
attribute: attribute,
relatedBy: .equal,
toItem: equalToView,
attribute: attribute,
multiplier: multiplier,
constant: 0)

public class func constraints(for view:UIView, filling:UIView) -> [NSLayoutConstraint] {
return [view.leadingAnchor.constraint(equalTo: filling.leadingAnchor),
view.trailingAnchor.constraint(equalTo: filling.trailingAnchor),
view.topAnchor.constraint(equalTo: filling.topAnchor),
view.bottomAnchor.constraint(equalTo: filling.bottomAnchor)]
}
}

// Apple hasn't bothered to make multiplicitave constraints for anchors that aren't dimensions...
// This weird function signature is working around a compiler bug.
private func con(straint constraint:NSLayoutConstraint, multiplier:CGFloat) -> NSLayoutConstraint {
return NSLayoutConstraint(item: constraint.firstItem, attribute: constraint.firstAttribute, relatedBy: constraint.relation, toItem: constraint.secondItem, attribute: constraint.secondAttribute, multiplier: multiplier, constant: constraint.constant)
}

public class func constraintFor(view:UIView, attribute:NSLayoutAttribute, lessThanOrEqualToView:UIView, multiplier:CGFloat) -> NSLayoutConstraint {
return NSLayoutConstraint(item: view,
attribute: attribute,
relatedBy: .lessThanOrEqual,
toItem: lessThanOrEqualToView,
attribute: attribute,
multiplier: multiplier,
constant: 0)
extension NSLayoutYAxisAnchor {
func constraint(equalTo anchor: NSLayoutAnchor<NSLayoutYAxisAnchor>, multiplier:CGFloat) -> NSLayoutConstraint {
return con(straint: super.constraint(equalTo: anchor), multiplier: multiplier)
}

public class func constraintFor(view:UIView, attribute:NSLayoutAttribute, greaterThanOrEqualToView:UIView, multiplier:CGFloat) -> NSLayoutConstraint {
return NSLayoutConstraint(item: view,
attribute: attribute,
relatedBy: .greaterThanOrEqual,
toItem: greaterThanOrEqualToView,
attribute: attribute,
multiplier: multiplier,
constant: 0)
func constraint(lessThanOrEqualTo anchor: NSLayoutAnchor<NSLayoutYAxisAnchor>, multiplier:CGFloat) -> NSLayoutConstraint {
return con(straint: super.constraint(lessThanOrEqualTo: anchor), multiplier: multiplier)
}

func constraint(greaterThanOrEqualTo anchor: NSLayoutAnchor<NSLayoutYAxisAnchor>, multiplier:CGFloat) -> NSLayoutConstraint {
return con(straint: super.constraint(greaterThanOrEqualTo: anchor), multiplier: multiplier)
}
}

public class func constraintsFor(view:UIView, fillingParentView:UIView) -> [NSLayoutConstraint] {
return [view.leadingAnchor.constraint(equalTo: fillingParentView.leadingAnchor),
view.trailingAnchor.constraint(equalTo: fillingParentView.trailingAnchor),
view.topAnchor.constraint(equalTo: fillingParentView.topAnchor),
view.bottomAnchor.constraint(equalTo: fillingParentView.bottomAnchor)]
extension NSLayoutXAxisAnchor {
func constraint(equalTo anchor: NSLayoutAnchor<NSLayoutXAxisAnchor>, multiplier:CGFloat) -> NSLayoutConstraint {
return con(straint: super.constraint(equalTo: anchor), multiplier: multiplier)
}

func constraint(lessThanOrEqualTo anchor: NSLayoutAnchor<NSLayoutXAxisAnchor>, multiplier:CGFloat) -> NSLayoutConstraint {
return con(straint: super.constraint(lessThanOrEqualTo: anchor), multiplier: multiplier)
}

func constraint(greaterThanOrEqualTo anchor: NSLayoutAnchor<NSLayoutXAxisAnchor>, multiplier:CGFloat) -> NSLayoutConstraint {
return con(straint: super.constraint(greaterThanOrEqualTo: anchor), multiplier: multiplier)
}
}

20 changes: 7 additions & 13 deletions Sources/OnboardingPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit

public
protocol OnboardingDoneDelegate {
protocol OnboardingDoneDelegate: AnyObject {
func donePressed(_ page:OnboardingPage)
}

Expand Down Expand Up @@ -120,7 +120,7 @@ class OnboardingContentPage : OnboardingPage {
backgroundImageView.contentMode = .scaleAspectFill

insertSubview(backgroundImageView, at: 0)
NSLayoutConstraint.activate((NSLayoutConstraint.constraintsFor(view: backgroundImageView, fillingParentView: self)))
NSLayoutConstraint.activate((NSLayoutConstraint.constraints(for: backgroundImageView, filling: self)))
}

private func setupTitleTopStyle() {
Expand Down Expand Up @@ -148,20 +148,14 @@ class OnboardingContentPage : OnboardingPage {
}

private func setupTitleSubordinateStyle() {

NSLayoutConstraint(
item: foregroundImageView,
attribute: .centerY,
relatedBy: .equal,
toItem: self,
attribute: .centerY,
multiplier: 0.66,
constant: 0).isActive = true

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

NSLayoutConstraint.activate([
titleLabel.centerYAnchor.constraint(equalTo: centerYAnchor, multiplier: 1.25),
foregroundImageView.centerYAnchor.constraint(equalTo: centerYAnchor, multiplier: 0.66)
])

setupCommonTitleConstraints()
setupCommonForegroundImageConstraints()
Expand Down Expand Up @@ -231,7 +225,7 @@ class OnboardingFinalPage : OnboardingContentPage {
override public func setupConstraints() {
super.setupConstraints()

let buttonYConstraint = NSLayoutConstraint.constraintFor(view: doneButton, attribute: .centerY, equalToView: self, multiplier: 1.7)
let buttonYConstraint = doneButton.centerYAnchor.constraint(equalTo: centerYAnchor, multiplier: 1.7)
buttonYConstraint.priority = UILayoutPriorityDefaultLow

NSLayoutConstraint.activate([
Expand Down
4 changes: 2 additions & 2 deletions Sources/OnboardingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class OnboardingViewController: UIViewController, UIScrollViewDelegate, Onboardi
pager.pageIndicatorTintColor = UIColor.lightGray()
overlayView.addSubview(pager)

NSLayoutConstraint.activate(NSLayoutConstraint.constraintsFor(view: overlayView, fillingParentView: view))
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(for: overlayView, filling: view))

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

NSLayoutConstraint.activate(NSLayoutConstraint.constraintsFor(view: scroller, fillingParentView: view))
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(for: scroller, filling: view))
}

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
Expand Down

0 comments on commit b3b5431

Please sign in to comment.