diff --git a/Onboarding.xcodeproj/project.pbxproj b/Onboarding.xcodeproj/project.pbxproj index 2626f9b..1261ebe 100644 --- a/Onboarding.xcodeproj/project.pbxproj +++ b/Onboarding.xcodeproj/project.pbxproj @@ -105,6 +105,7 @@ E755CDA91CAF236900980DE8 = { CreatedOnToolsVersion = 7.3; DevelopmentTeam = SR7K2S8GE4; + LastSwiftMigration = 0800; }; }; }; @@ -258,6 +259,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -277,6 +279,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.rockwood.Onboarding; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/Sources/NSLayoutConstraint+Convenience.swift b/Sources/NSLayoutConstraint+Convenience.swift index f9f8bcf..b518cf0 100644 --- a/Sources/NSLayoutConstraint+Convenience.swift +++ b/Sources/NSLayoutConstraint+Convenience.swift @@ -11,40 +11,40 @@ import UIKit extension NSLayoutConstraint { // Apple hasn't bothered to make multiplicitave constraints for anchors that aren't dimensions... - public class func constraintFor(view view:UIView, attribute:NSLayoutAttribute, equalToView:UIView, multiplier:CGFloat) -> NSLayoutConstraint { + public class func constraintFor(view:UIView, attribute:NSLayoutAttribute, equalToView:UIView, multiplier:CGFloat) -> NSLayoutConstraint { return NSLayoutConstraint(item: view, attribute: attribute, - relatedBy: .Equal, + relatedBy: .equal, toItem: equalToView, attribute: attribute, multiplier: multiplier, constant: 0) } - public class func constraintFor(view view:UIView, attribute:NSLayoutAttribute, lessThanOrEqualToView:UIView, multiplier:CGFloat) -> NSLayoutConstraint { + public class func constraintFor(view:UIView, attribute:NSLayoutAttribute, lessThanOrEqualToView:UIView, multiplier:CGFloat) -> NSLayoutConstraint { return NSLayoutConstraint(item: view, attribute: attribute, - relatedBy: .LessThanOrEqual, + relatedBy: .lessThanOrEqual, toItem: lessThanOrEqualToView, attribute: attribute, multiplier: multiplier, constant: 0) } - public class func constraintFor(view view:UIView, attribute:NSLayoutAttribute, greaterThanOrEqualToView:UIView, multiplier:CGFloat) -> NSLayoutConstraint { + public class func constraintFor(view:UIView, attribute:NSLayoutAttribute, greaterThanOrEqualToView:UIView, multiplier:CGFloat) -> NSLayoutConstraint { return NSLayoutConstraint(item: view, attribute: attribute, - relatedBy: .GreaterThanOrEqual, + relatedBy: .greaterThanOrEqual, toItem: greaterThanOrEqualToView, attribute: attribute, multiplier: multiplier, constant: 0) } - public class func constraintsFor(view view:UIView, fillingParentView:UIView) -> [NSLayoutConstraint] { - return [view.leadingAnchor.constraintEqualToAnchor(fillingParentView.leadingAnchor), - view.trailingAnchor.constraintEqualToAnchor(fillingParentView.trailingAnchor), - view.topAnchor.constraintEqualToAnchor(fillingParentView.topAnchor), - view.bottomAnchor.constraintEqualToAnchor(fillingParentView.bottomAnchor)] + 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)] } -} \ No newline at end of file +} diff --git a/Sources/OnboardingPage.swift b/Sources/OnboardingPage.swift index 482a693..40a2a9a 100644 --- a/Sources/OnboardingPage.swift +++ b/Sources/OnboardingPage.swift @@ -10,7 +10,7 @@ import UIKit public protocol OnboardingDoneDelegate { - func donePressed(page:OnboardingPage) + func donePressed(_ page:OnboardingPage) } private let kMinimumElementSpacing:CGFloat = 10 @@ -20,7 +20,7 @@ public class OnboardingPage : UIView { init() { - super.init(frame:CGRectZero) + super.init(frame:CGRect.zero) translatesAutoresizingMaskIntoConstraints = false } @@ -69,16 +69,16 @@ class OnboardingContentPage : OnboardingPage { titleLabel.translatesAutoresizingMaskIntoConstraints = false titleLabel.text = titleText - titleLabel.font = UIFont.boldSystemFontOfSize(36) + titleLabel.font = UIFont.boldSystemFont(ofSize: 36) titleLabel.adjustsFontSizeToFitWidth = true - titleLabel.textAlignment = .Center + titleLabel.textAlignment = .center foregroundImageView.translatesAutoresizingMaskIntoConstraints = false foregroundImageView.image = foregroundImage - foregroundImageView.contentMode = .ScaleAspectFit + foregroundImageView.contentMode = .scaleAspectFit contentLabel.translatesAutoresizingMaskIntoConstraints = false - contentLabel.textAlignment = .Center + contentLabel.textAlignment = .center contentLabel.text = contentText contentLabel.numberOfLines = 0 @@ -114,32 +114,32 @@ class OnboardingContentPage : OnboardingPage { } } - private func setupBackgroundImage(image:UIImage) { + private func setupBackgroundImage(_ image:UIImage) { backgroundImageView.image = image backgroundImageView.translatesAutoresizingMaskIntoConstraints = false - backgroundImageView.contentMode = .ScaleAspectFill + backgroundImageView.contentMode = .scaleAspectFill - insertSubview(backgroundImageView, atIndex: 0) - NSLayoutConstraint.activateConstraints((NSLayoutConstraint.constraintsFor(view: backgroundImageView, fillingParentView: self))) + insertSubview(backgroundImageView, at: 0) + NSLayoutConstraint.activate((NSLayoutConstraint.constraintsFor(view: backgroundImageView, fillingParentView: self))) } private func setupTitleTopStyle() { // TODO: Center title between top and foreground image - let imageCenterYConstraint = foregroundImageView.centerYAnchor.constraintEqualToAnchor(centerYAnchor) + let imageCenterYConstraint = foregroundImageView.centerYAnchor.constraint(equalTo: centerYAnchor) imageCenterYConstraint.priority = UILayoutPriorityDefaultLow - NSLayoutConstraint.activateConstraints([ + NSLayoutConstraint.activate([ NSLayoutConstraint( item: titleLabel, - attribute: .CenterY, - relatedBy: .Equal, + attribute: .centerY, + relatedBy: .equal, toItem: self, - attribute: .CenterY, + attribute: .centerY, multiplier: 0.5, constant: 0), // WTF, Apple, no multiplier on Anchors that aren't dimensions? imageCenterYConstraint, - foregroundImageView.topAnchor.constraintGreaterThanOrEqualToAnchor(titleLabel.bottomAnchor, constant: kMinimumElementSpacing) + foregroundImageView.topAnchor.constraint(greaterThanOrEqualTo: titleLabel.bottomAnchor, constant: kMinimumElementSpacing) ]) setupCommonTitleConstraints() @@ -151,16 +151,16 @@ class OnboardingContentPage : OnboardingPage { NSLayoutConstraint( item: foregroundImageView, - attribute: .CenterY, - relatedBy: .Equal, + attribute: .centerY, + relatedBy: .equal, toItem: self, - attribute: .CenterY, + attribute: .centerY, multiplier: 0.66, - constant: 0).active = true + constant: 0).isActive = true - titleLabel.font = UIFont.systemFontOfSize(24) + titleLabel.font = UIFont.systemFont(ofSize: 24) titleLabel.numberOfLines = 0 - NSLayoutConstraint.constraintFor(view: titleLabel, attribute: .CenterY, equalToView: self, multiplier: 1.25).active = true + NSLayoutConstraint.constraintFor(view: titleLabel, attribute: .centerY, equalToView: self, multiplier: 1.25).isActive = true setupCommonTitleConstraints() @@ -169,43 +169,43 @@ class OnboardingContentPage : OnboardingPage { } private func setupCommonTitleConstraints() { - NSLayoutConstraint.activateConstraints([ - titleLabel.centerXAnchor.constraintEqualToAnchor(centerXAnchor), - titleLabel.widthAnchor.constraintEqualToAnchor(widthAnchor, constant: kWidthOffsetConstant) + NSLayoutConstraint.activate([ + titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor), + titleLabel.widthAnchor.constraint(equalTo: widthAnchor, constant: kWidthOffsetConstant) ]) } private func setupCommonForegroundImageConstraints() { if let image = foregroundImageView.image { let aspect = image.size.height / image.size.width - foregroundImageView.heightAnchor.constraintEqualToAnchor(foregroundImageView.widthAnchor, multiplier: aspect).active = true + foregroundImageView.heightAnchor.constraint(equalTo: foregroundImageView.widthAnchor, multiplier: aspect).isActive = true } - NSLayoutConstraint.activateConstraints([ - foregroundImageView.centerXAnchor.constraintEqualToAnchor(centerXAnchor), - foregroundImageView.widthAnchor.constraintLessThanOrEqualToAnchor(widthAnchor, constant: kWidthOffsetConstant), + NSLayoutConstraint.activate([ + foregroundImageView.centerXAnchor.constraint(equalTo: centerXAnchor), + foregroundImageView.widthAnchor.constraint(lessThanOrEqualTo: widthAnchor, constant: kWidthOffsetConstant), ]) } private func setupContentConstraints() { let contentMidYConstraint = NSLayoutConstraint(item: contentLabel, - attribute: .CenterY, - relatedBy: .Equal, + attribute: .centerY, + relatedBy: .equal, toItem: self, - attribute: .CenterY, + attribute: .centerY, multiplier: 1.5, constant: 0) contentMidYConstraint.priority = UILayoutPriorityDefaultLow - let contentBottomConstraint = contentLabel.bottomAnchor.constraintEqualToAnchor(bottomAnchor) - contentBottomConstraint.constant = -UIScreen.mainScreen().bounds.height * 0.1 + let contentBottomConstraint = contentLabel.bottomAnchor.constraint(equalTo: bottomAnchor) + contentBottomConstraint.constant = -UIScreen.main().bounds.height * 0.1 contentBottomConstraint.priority = UILayoutPriorityDefaultLow - 1 - NSLayoutConstraint.activateConstraints([ + NSLayoutConstraint.activate([ contentMidYConstraint, contentBottomConstraint, - contentLabel.widthAnchor.constraintEqualToAnchor(widthAnchor, constant: kWidthOffsetConstant), - contentLabel.centerXAnchor.constraintEqualToAnchor(centerXAnchor) + contentLabel.widthAnchor.constraint(equalTo: widthAnchor, constant: kWidthOffsetConstant), + contentLabel.centerXAnchor.constraint(equalTo: centerXAnchor) ]) } } @@ -216,14 +216,14 @@ class OnboardingContentPage : OnboardingPage { public class OnboardingFinalPage : OnboardingContentPage { - public var doneButton = UIButton(type: .System) - public var doneDelegate:OnboardingDoneDelegate? = nil + public var doneButton = UIButton(type: .system) + public weak var doneDelegate:OnboardingDoneDelegate? = nil override public func addSubviews() { super.addSubviews() - doneButton.setTitle("Make It So!", forState: .Normal) + doneButton.setTitle("Make It So!", for: UIControlState()) doneButton.translatesAutoresizingMaskIntoConstraints = false - doneButton.addTarget(self, action: #selector(donePressed(_:)), forControlEvents: .TouchUpInside) + doneButton.addTarget(self, action: #selector(donePressed(_:)), for: .touchUpInside) addSubview(doneButton) } @@ -231,17 +231,17 @@ class OnboardingFinalPage : OnboardingContentPage { override public func setupConstraints() { super.setupConstraints() - let buttonYConstraint = NSLayoutConstraint.constraintFor(view: doneButton, attribute: .CenterY, equalToView: self, multiplier: 1.7) + let buttonYConstraint = NSLayoutConstraint.constraintFor(view: doneButton, attribute: .centerY, equalToView: self, multiplier: 1.7) buttonYConstraint.priority = UILayoutPriorityDefaultLow - NSLayoutConstraint.activateConstraints([ - doneButton.centerXAnchor.constraintEqualToAnchor(centerXAnchor), + NSLayoutConstraint.activate([ + doneButton.centerXAnchor.constraint(equalTo: centerXAnchor), buttonYConstraint, - doneButton.topAnchor.constraintGreaterThanOrEqualToAnchor(contentLabel.bottomAnchor, constant: kMinimumElementSpacing) + doneButton.topAnchor.constraint(greaterThanOrEqualTo: contentLabel.bottomAnchor, constant: kMinimumElementSpacing) ]) } - @objc private func donePressed(button:UIButton) { + @objc private func donePressed(_ button:UIButton) { doneDelegate?.donePressed(self) } } diff --git a/Sources/OnboardingViewController.swift b/Sources/OnboardingViewController.swift index 2e14355..5471de8 100644 --- a/Sources/OnboardingViewController.swift +++ b/Sources/OnboardingViewController.swift @@ -27,7 +27,7 @@ public class OnboardingViewController: UIViewController, UIScrollViewDelegate, OnboardingDoneDelegate { public - var skipButton = UIButton(type: .System) { + var skipButton = UIButton(type: .system) { willSet { currentSkipConstraints = [] skipButton.removeFromSuperview() @@ -71,7 +71,7 @@ class OnboardingViewController: UIViewController, UIScrollViewDelegate, Onboardi } } - public func setPages(pageset:[OnboardingPage]) { + public func setPages(_ pageset:[OnboardingPage]) { pages.forEach { $0.removeFromSuperview() } @@ -86,20 +86,20 @@ class OnboardingViewController: UIViewController, UIScrollViewDelegate, Onboardi currentPageIndex = 0 var anchorView:UIView = scroller - var anchorAttribute = NSLayoutAttribute.Left + var anchorAttribute = NSLayoutAttribute.left pages.forEach { (page) in scroller.addSubview(page) - NSLayoutConstraint.activateConstraints([ - page.heightAnchor.constraintEqualToAnchor(scroller.heightAnchor), - page.centerYAnchor.constraintEqualToAnchor(scroller.centerYAnchor), - page.widthAnchor.constraintEqualToAnchor(scroller.widthAnchor) + NSLayoutConstraint.activate([ + page.heightAnchor.constraint(equalTo: scroller.heightAnchor), + page.centerYAnchor.constraint(equalTo: scroller.centerYAnchor), + page.widthAnchor.constraint(equalTo: scroller.widthAnchor) ]) scroller.addConstraint( - NSLayoutConstraint(item: page, attribute: .Left, relatedBy: .Equal, toItem: anchorView, attribute: anchorAttribute, multiplier: 1, constant: 0)) + NSLayoutConstraint(item: page, attribute: .left, relatedBy: .equal, toItem: anchorView, attribute: anchorAttribute, multiplier: 1, constant: 0)) anchorView = page - anchorAttribute = .Right + anchorAttribute = .right } if let finalPage = pages.last as? OnboardingFinalPage { @@ -107,12 +107,12 @@ class OnboardingViewController: UIViewController, UIScrollViewDelegate, Onboardi } // ScrollViews and autolayout is ... weird and special. - scroller.addConstraint(NSLayoutConstraint(item: scroller, attribute: .Right, relatedBy: .Equal, toItem: anchorView, attribute: .Right, multiplier: 1, constant: 0)) + scroller.addConstraint(NSLayoutConstraint(item: scroller, attribute: .right, relatedBy: .equal, toItem: anchorView, attribute: .right, multiplier: 1, constant: 0)) } override public func viewDidLoad() { super.viewDidLoad() - view.backgroundColor = UIColor.whiteColor() + view.backgroundColor = UIColor.white() setupOverlay() setupSkipButton() @@ -128,7 +128,7 @@ class OnboardingViewController: UIViewController, UIScrollViewDelegate, Onboardi return true } - @objc private func skipButtonPressed(button:UIButton) { + @objc private func skipButtonPressed(_ button:UIButton) { guard let skipActionActual = skipAction else { // This is supposed to crash with this message. @@ -140,14 +140,14 @@ class OnboardingViewController: UIViewController, UIScrollViewDelegate, Onboardi } private func setupSkipButton() { - skipButton.setTitle(NSLocalizedString("Skip", comment:"Onboarding 'Skip' button"), forState: .Normal) + skipButton.setTitle(NSLocalizedString("Skip", comment:"Onboarding 'Skip' button"), for: UIControlState()) skipButton.translatesAutoresizingMaskIntoConstraints = false updateSkipButton() } // Nomenclature is hard private func updateSkipButton() { - skipButton.addTarget(self, action: #selector(skipButtonPressed), forControlEvents: .TouchUpInside) + skipButton.addTarget(self, action: #selector(skipButtonPressed), for: .touchUpInside) overlayView.addSubview(skipButton) updateSkipConstraints() @@ -157,24 +157,24 @@ class OnboardingViewController: UIViewController, UIScrollViewDelegate, Onboardi overlayView.removeConstraints(currentSkipConstraints) - var firstAttribute = NSLayoutAttribute.Left - var secondAttribute = NSLayoutAttribute.Bottom + var firstAttribute = NSLayoutAttribute.left + var secondAttribute = NSLayoutAttribute.bottom var firstInset = skStandardHInset var secondInset = -skStandardVInset switch skipPosition { case .topLeft: - secondAttribute = .Top + secondAttribute = .top secondInset = skStandardVInset case .topRight: - firstAttribute = .Right - secondAttribute = .Top + firstAttribute = .right + secondAttribute = .top firstInset = -skStandardHInset secondInset = skStandardVInset case .bottomRight: - firstAttribute = .Right + firstAttribute = .right firstInset = -skStandardHInset case .bottomLeft: @@ -187,54 +187,54 @@ class OnboardingViewController: UIViewController, UIScrollViewDelegate, Onboardi currentSkipConstraints = [ NSLayoutConstraint(item: skipButton, attribute: firstAttribute, - relatedBy: .Equal, + relatedBy: .equal, toItem: overlayView, attribute: firstAttribute, multiplier: 1, constant: firstInset), NSLayoutConstraint(item: skipButton, attribute: secondAttribute, - relatedBy: .Equal, + relatedBy: .equal, toItem: overlayView, attribute: secondAttribute, multiplier: 1, constant: secondInset) ] - NSLayoutConstraint.activateConstraints(currentSkipConstraints) + NSLayoutConstraint.activate(currentSkipConstraints) } private func setupOverlay() { view.addSubview(overlayView) - view.bringSubviewToFront(overlayView) + view.bringSubview(toFront: overlayView) overlayView.translatesAutoresizingMaskIntoConstraints = false pager.translatesAutoresizingMaskIntoConstraints = false - pager.currentPageIndicatorTintColor = UIColor.blackColor() - pager.pageIndicatorTintColor = UIColor.lightGrayColor() + pager.currentPageIndicatorTintColor = UIColor.black() + pager.pageIndicatorTintColor = UIColor.lightGray() overlayView.addSubview(pager) - NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsFor(view: overlayView, fillingParentView: view)) + NSLayoutConstraint.activate(NSLayoutConstraint.constraintsFor(view: overlayView, fillingParentView: view)) - NSLayoutConstraint.activateConstraints([ - pager.leadingAnchor.constraintEqualToAnchor(overlayView.leadingAnchor), - pager.trailingAnchor.constraintEqualToAnchor(overlayView.trailingAnchor), - pager.bottomAnchor.constraintEqualToAnchor(overlayView.bottomAnchor) + NSLayoutConstraint.activate([ + pager.leadingAnchor.constraint(equalTo: overlayView.leadingAnchor), + pager.trailingAnchor.constraint(equalTo: overlayView.trailingAnchor), + pager.bottomAnchor.constraint(equalTo: overlayView.bottomAnchor) ]) } private func setupScroller() { scroller.translatesAutoresizingMaskIntoConstraints = false - scroller.pagingEnabled = true + scroller.isPagingEnabled = true scroller.delegate = self scroller.showsHorizontalScrollIndicator = false view.addSubview(scroller) - NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsFor(view: scroller, fillingParentView: view)) + NSLayoutConstraint.activate(NSLayoutConstraint.constraintsFor(view: scroller, fillingParentView: view)) } - public func scrollViewDidScroll(scrollView: UIScrollView) { + public func scrollViewDidScroll(_ scrollView: UIScrollView) { let pageWidth = view.frame.width let halfWidth = pageWidth / 2 let pageNum = Int( ( scroller.contentOffset.x + halfWidth ) / pageWidth ) @@ -246,7 +246,7 @@ class OnboardingViewController: UIViewController, UIScrollViewDelegate, Onboardi // MARK: - OnboardingDoneDelegate - public func donePressed(page: OnboardingPage) { + public func donePressed(_ page: OnboardingPage) { doneAction?(onboardingController: self) } }