Skip to content

Commit

Permalink
bug fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyyamagu committed Dec 29, 2015
1 parent 7e6595e commit 0d2c11f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
25 changes: 8 additions & 17 deletions KYNavigationProgress/ProgressView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
}


Expand Down
41 changes: 35 additions & 6 deletions KYNavigationProgress/UINavigationController+Progress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import UIKit

private let constraintIdentifier = "progressHeightConstraint"

public extension UINavigationController {

/* ====================================================================== */
Expand All @@ -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
}
}

Expand Down Expand Up @@ -63,7 +68,6 @@ public extension UINavigationController {
}

let defaultHeight = CGFloat(2)

let frame = CGRect(
x: 0,
y: navigationBar.frame.height - defaultHeight,
Expand All @@ -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
}

Expand All @@ -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,
Expand Down

0 comments on commit 0d2c11f

Please sign in to comment.