Skip to content

Commit

Permalink
added UIAppearance compatibility
Browse files Browse the repository at this point in the history
by making ProgressView public and the color variables dynamic, users
can set global track and progress tint colors via
```ProgressView.appearance().trackTintColor = …```
  • Loading branch information
imaccallum committed Dec 28, 2016
1 parent 2383b22 commit d0ee33d
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 185 deletions.
11 changes: 9 additions & 2 deletions KYNavigationProgress.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,13 @@
TargetAttributes = {
C2A91C121C32782F006D1CAE = {
CreatedOnToolsVersion = 7.2;
DevelopmentTeam = 432FNJ7C99;
LastSwiftMigration = 0800;
ProvisioningStyle = Automatic;
};
C2A91C301C327872006D1CAE = {
CreatedOnToolsVersion = 7.2;
DevelopmentTeam = 432FNJ7C99;
};
};
};
Expand Down Expand Up @@ -377,8 +380,9 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = 432FNJ7C99;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand All @@ -398,8 +402,9 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = 432FNJ7C99;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand All @@ -419,6 +424,7 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 432FNJ7C99;
INFOPLIST_FILE = Sample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -432,6 +438,7 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 432FNJ7C99;
INFOPLIST_FILE = Sample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down
232 changes: 120 additions & 112 deletions KYNavigationProgress/ProgressView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,116 +8,124 @@

import UIKit

internal final class ProgressView: UIView {

/* ====================================================================== */
// MARK: - Properties
/* ====================================================================== */

internal var progress: Float = 0 {
didSet {
progress = min(1, progress)
barWidthConstraint.constant = bounds.width * CGFloat(progress)
}
}

internal let bar = UIView()

fileprivate let defaultBarColor = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1)

fileprivate let defaultTrackColor = UIColor.clear

fileprivate let barWidthConstraint: NSLayoutConstraint

override var frame: CGRect {
didSet {
let tmpProgress = progress
progress = tmpProgress
}
}


/* ====================================================================== */
// MARK: - initializer
/* ====================================================================== */

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override init(frame: CGRect) {
barWidthConstraint = NSLayoutConstraint(
item: bar,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: frame.width * CGFloat(progress))

super.init(frame: frame)

let leftConstraint = NSLayoutConstraint(
item: bar,
attribute: .left,
relatedBy: .equal,
toItem: self,
attribute: .left,
multiplier: 1,
constant: 0)

let bottomConstraint = NSLayoutConstraint(
item: bar,
attribute: .bottom,
relatedBy: .equal,
toItem: self,
attribute: .bottom,
multiplier: 1,
constant: 0)

let topConstraint = NSLayoutConstraint(
item: bar,
attribute: .top,
relatedBy: .equal,
toItem: self,
attribute: .top,
multiplier: 1,
constant: 0)

addSubview(bar)

backgroundColor = defaultTrackColor

bar.backgroundColor = defaultBarColor
bar.translatesAutoresizingMaskIntoConstraints = false
addConstraints([
barWidthConstraint,
leftConstraint,
topConstraint,
bottomConstraint])
}


/* ====================================================================== */
// MARK: - Notification
/* ====================================================================== */

func deviceDidRotate(_ notification: Notification) {
}


/* ====================================================================== */
// MARK: - Method
/* ====================================================================== */

internal func setProgress(_ progress: Float, animated: Bool) {
let duration: TimeInterval = animated ? 0.1 : 0

self.progress = progress

UIView.animate(withDuration: duration, animations: {
self.layoutIfNeeded()
})
}

public final class ProgressView: UIView {

/* ====================================================================== */
// MARK: - Properties
/* ====================================================================== */

internal var progress: Float = 0 {
didSet {
progress = min(1, progress)
barWidthConstraint.constant = bounds.width * CGFloat(progress)
}
}

internal let bar = UIView()

public dynamic var progressTintColor: UIColor? = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1) {
didSet {
bar.backgroundColor = progressTintColor
}
}

public dynamic var trackTintColor: UIColor? = .clear {
didSet {
backgroundColor = trackTintColor
}
}

fileprivate let barWidthConstraint: NSLayoutConstraint

override public var frame: CGRect {
didSet {
let tmpProgress = progress
progress = tmpProgress
}
}


/* ====================================================================== */
// MARK: - initializer
/* ====================================================================== */

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override init(frame: CGRect) {
barWidthConstraint = NSLayoutConstraint(
item: bar,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: frame.width * CGFloat(progress))

super.init(frame: frame)

let leftConstraint = NSLayoutConstraint(
item: bar,
attribute: .left,
relatedBy: .equal,
toItem: self,
attribute: .left,
multiplier: 1,
constant: 0)

let bottomConstraint = NSLayoutConstraint(
item: bar,
attribute: .bottom,
relatedBy: .equal,
toItem: self,
attribute: .bottom,
multiplier: 1,
constant: 0)

let topConstraint = NSLayoutConstraint(
item: bar,
attribute: .top,
relatedBy: .equal,
toItem: self,
attribute: .top,
multiplier: 1,
constant: 0)

addSubview(bar)

backgroundColor = trackTintColor

bar.backgroundColor = progressTintColor
bar.translatesAutoresizingMaskIntoConstraints = false
addConstraints([
barWidthConstraint,
leftConstraint,
topConstraint,
bottomConstraint])
}


/* ====================================================================== */
// MARK: - Notification
/* ====================================================================== */

func deviceDidRotate(_ notification: Notification) {
}


/* ====================================================================== */
// MARK: - Method
/* ====================================================================== */

internal func setProgress(_ progress: Float, animated: Bool) {
let duration: TimeInterval = animated ? 0.1 : 0

self.progress = progress

UIView.animate(withDuration: duration, animations: {
self.layoutIfNeeded()
})
}

}
8 changes: 4 additions & 4 deletions KYNavigationProgress/UINavigationController+Progress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public extension UINavigationController {
default is clear color.
*/
public var trackTintColor: UIColor? {
get { return progressView.backgroundColor }
set { progressView.backgroundColor = newValue }
get { return progressView.trackTintColor }
set { progressView.trackTintColor = newValue }
}

/**
The color shown for the portion of the progress bar that is filled.
default is (r: 0, g: 122, b: 225, a: 255.
*/
public var progressTintColor: UIColor? {
get { return progressView.bar.backgroundColor }
set { progressView.bar.backgroundColor = newValue }
get { return progressView.progressTintColor }
set { progressView.progressTintColor = newValue }
}

/**
Expand Down
67 changes: 36 additions & 31 deletions Sample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,44 @@
//

import UIKit
import KYNavigationProgress

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}



var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
// Override point for customization after application launch.

ProgressView.appearance().progressTintColor = .green
ProgressView.appearance().trackTintColor = .red

return true
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

Loading

0 comments on commit d0ee33d

Please sign in to comment.