From d0ee33dc0f34014bfc55ff81cb62edf8e87701a3 Mon Sep 17 00:00:00 2001 From: Ian MacCallum Date: Tue, 27 Dec 2016 21:45:05 -0500 Subject: [PATCH] added UIAppearance compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit by making ProgressView public and the color variables dynamic, users can set global track and progress tint colors via ```ProgressView.appearance().trackTintColor = …``` --- .../project.pbxproj | 11 +- KYNavigationProgress/ProgressView.swift | 232 +++++++++--------- .../UINavigationController+Progress.swift | 8 +- Sample/AppDelegate.swift | 67 ++--- Sample/ViewController.swift | 68 +++-- 5 files changed, 201 insertions(+), 185 deletions(-) diff --git a/KYNavigationProgress.xcodeproj/project.pbxproj b/KYNavigationProgress.xcodeproj/project.pbxproj index a6f3347..e24bfb9 100644 --- a/KYNavigationProgress.xcodeproj/project.pbxproj +++ b/KYNavigationProgress.xcodeproj/project.pbxproj @@ -182,10 +182,13 @@ TargetAttributes = { C2A91C121C32782F006D1CAE = { CreatedOnToolsVersion = 7.2; + DevelopmentTeam = 432FNJ7C99; LastSwiftMigration = 0800; + ProvisioningStyle = Automatic; }; C2A91C301C327872006D1CAE = { CreatedOnToolsVersion = 7.2; + DevelopmentTeam = 432FNJ7C99; }; }; }; @@ -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"; @@ -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"; @@ -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"; @@ -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"; diff --git a/KYNavigationProgress/ProgressView.swift b/KYNavigationProgress/ProgressView.swift index 6c76bde..4a745ff 100644 --- a/KYNavigationProgress/ProgressView.swift +++ b/KYNavigationProgress/ProgressView.swift @@ -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() + }) + } + } diff --git a/KYNavigationProgress/UINavigationController+Progress.swift b/KYNavigationProgress/UINavigationController+Progress.swift index 28a0bb0..cf442ca 100644 --- a/KYNavigationProgress/UINavigationController+Progress.swift +++ b/KYNavigationProgress/UINavigationController+Progress.swift @@ -32,8 +32,8 @@ 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 } } /** @@ -41,8 +41,8 @@ public extension UINavigationController { 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 } } /** diff --git a/Sample/AppDelegate.swift b/Sample/AppDelegate.swift index edf4450..bb557f2 100644 --- a/Sample/AppDelegate.swift +++ b/Sample/AppDelegate.swift @@ -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:. + } + + } diff --git a/Sample/ViewController.swift b/Sample/ViewController.swift index 04a79db..3fa5b5a 100644 --- a/Sample/ViewController.swift +++ b/Sample/ViewController.swift @@ -10,41 +10,37 @@ import UIKit import KYNavigationProgress class ViewController: UIViewController { - - @IBAction func didTapUpButton(sender: UIButton) { - let progress = navigationController!.progress - navigationController!.setProgress(progress + 0.1, animated: true) - } - - @IBAction func didTapFinishButton(sender: UIButton) { - navigationController!.finishProgress() - } - - @IBAction func didTapCancelButton(sender: UIButton) { - navigationController!.cancelProgress() - } - - @IBAction func handleValueChanged(sender: UISegmentedControl) { - switch sender.selectedSegmentIndex { - case 0: navigationController?.progressTintColor = UIColor.red - case 1: navigationController?.progressTintColor = UIColor.green - case 2: navigationController?.progressTintColor = UIColor.blue - default: break - } - } - - - override func viewDidLoad() { - super.viewDidLoad() - // Do any additional setup after loading the view, typically from a nib. - navigationController?.progressHeight = 2 - } - - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. - } - - + + @IBAction func didTapUpButton(sender: UIButton) { + let progress = navigationController!.progress + navigationController!.setProgress(progress + 0.1, animated: true) + } + + @IBAction func didTapFinishButton(sender: UIButton) { + navigationController!.finishProgress() + } + + @IBAction func didTapCancelButton(sender: UIButton) { + navigationController!.cancelProgress() + } + + @IBAction func handleValueChanged(sender: UISegmentedControl) { + } + + + override func viewDidLoad() { + super.viewDidLoad() + // Do any additional setup after loading the view, typically from a nib. + navigationController?.progressHeight = 2 + navigationController?.progressTintColor = .yellow + navigationController?.trackTintColor = .blue + } + + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + }