diff --git a/KYNavigationProgress/ProgressView.swift b/KYNavigationProgress/ProgressView.swift index cd320f0..3e79204 100644 --- a/KYNavigationProgress/ProgressView.swift +++ b/KYNavigationProgress/ProgressView.swift @@ -16,9 +16,8 @@ internal final class ProgressView: UIView { internal var progress: Float = 0 { didSet { - guard let superview = superview else { return } progress = min(1, progress) - barWidthConstraint.constant = superview.frame.width * CGFloat(progress) + barWidthConstraint.constant = bounds.width * CGFloat(progress) } } @@ -30,6 +29,13 @@ internal final class ProgressView: UIView { private let barWidthConstraint: NSLayoutConstraint + override var bounds: CGRect { + didSet { + let tmpProgress = progress + progress = tmpProgress + } + } + /* ====================================================================== */ // MARK: - initializer @@ -51,12 +57,6 @@ internal final class ProgressView: UIView { super.init(frame: frame) - NSNotificationCenter.defaultCenter().addObserver( - self, - selector: "deviceDidRotate:", - name: UIDeviceOrientationDidChangeNotification, - object: nil) - let leftConstraint = NSLayoutConstraint( item: bar, attribute: .Left, @@ -97,21 +97,12 @@ internal final class ProgressView: UIView { bottomConstraint]) } - deinit { - NSNotificationCenter.defaultCenter().removeObserver( - self, - name: UIDeviceOrientationDidChangeNotification, - object: nil) - } - /* ====================================================================== */ // MARK: - Notification /* ====================================================================== */ func deviceDidRotate(notification: NSNotification) { - let tmpProgress = progress - progress = tmpProgress } diff --git a/KYNavigationProgress/UINavigationController+Progress.swift b/KYNavigationProgress/UINavigationController+Progress.swift index 8273f8c..6dce16a 100644 --- a/KYNavigationProgress/UINavigationController+Progress.swift +++ b/KYNavigationProgress/UINavigationController+Progress.swift @@ -8,6 +8,8 @@ import UIKit +private let constraintIdentifier = "progressHeightConstraint" + public extension UINavigationController { /* ====================================================================== */ @@ -18,12 +20,15 @@ public extension UINavigationController { Default is 2.0 */ public var progressHeight: CGFloat { - get { return progressView.frame.height } + get { + return navigationBar.constraints + .filter{ $0.identifier == constraintIdentifier } + .first?.constant ?? 0.2 + } set { - var frame = progressView.frame - frame.size.height = newValue - frame.origin.y = navigationBar.frame.height - newValue - progressView.frame = frame + navigationBar.constraints + .filter{ $0.identifier == constraintIdentifier } + .first?.constant = newValue } } @@ -63,7 +68,6 @@ public extension UINavigationController { } let defaultHeight = CGFloat(2) - let frame = CGRect( x: 0, y: navigationBar.frame.height - defaultHeight, @@ -72,8 +76,32 @@ public extension UINavigationController { ) let progressView = ProgressView(frame: frame) + progressView.translatesAutoresizingMaskIntoConstraints = false + navigationBar.addSubview(progressView) + let progressHeightConstraint = NSLayoutConstraint( + item: progressView, + attribute: .Height, + relatedBy: .Equal, + toItem: nil, + attribute: .NotAnAttribute, + multiplier: 1, + constant: defaultHeight) + progressHeightConstraint.identifier = constraintIdentifier + + navigationBar.addConstraint(progressHeightConstraint) + navigationBar.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( + "H:|-0-[progressView]-0-|", + options: [], + metrics: nil, + views: ["progressView" : progressView])) + navigationBar.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( + "V:[progressView]-0-|", + options: [], + metrics: nil, + views: ["progressView" : progressView])) + return progressView } @@ -97,6 +125,7 @@ public extension UINavigationController { While progress is changed to 1.0, the bar will fade out. After that, progress will be 0.0. */ public func finishProgress() { + progressView.bar.alpha = 1 progressView.setProgress(1, animated: true) UIView.animateWithDuration(0.25,