|
// |
|
// UINavigationbar+Looks.swift |
|
// |
|
// License: MIT |
|
// |
|
|
|
import UIKit |
|
|
|
/** |
|
Stores information about navigation bar looks. |
|
*/ |
|
struct NavigationBarLooks { |
|
var backIndicatorImage: UIImage? |
|
var backIndicatatorTransitionImageMask: UIImage? |
|
var barStyle: UIBarStyle? |
|
var barTintColor: UIColor? |
|
var shadowImage: UIImage? |
|
var tintColor: UIColor? |
|
var translucent: Bool? |
|
var backgroundImageMetrics: [UIBarMetrics: UIImage?]? |
|
var titleVerticalPosition: [UIBarMetrics: CGFloat]? |
|
var titleTextAttributes: [String: AnyObject]? |
|
|
|
init(backIndicatorImage: UIImage? = nil, |
|
backIndicatatorTransitionImageMask: UIImage? = nil, |
|
barStyle: UIBarStyle = UIBarStyle.Default, |
|
barTintColor: UIColor? = nil, |
|
shadowImage: UIImage? = nil, |
|
tintColor: UIColor? = nil, |
|
translucent: Bool = false, |
|
backgroundImageMetrics: [UIBarMetrics: UIImage?]? = [:], |
|
titleVerticalPosition: [UIBarMetrics: CGFloat]? = [:], |
|
titleTextAttributes: [String: AnyObject]? = [:]) |
|
{ |
|
self.backIndicatorImage = backIndicatorImage |
|
self.backIndicatatorTransitionImageMask = backIndicatatorTransitionImageMask |
|
self.barStyle = barStyle |
|
self.barTintColor = barTintColor |
|
self.shadowImage = shadowImage |
|
self.tintColor = tintColor |
|
self.translucent = translucent |
|
self.backgroundImageMetrics = backgroundImageMetrics |
|
self.titleVerticalPosition = titleVerticalPosition |
|
self.titleTextAttributes = titleTextAttributes |
|
} |
|
} |
|
|
|
private var metrics = [UIBarMetrics.Default, UIBarMetrics.DefaultPrompt, UIBarMetrics.Compact, UIBarMetrics.CompactPrompt] |
|
|
|
extension UINavigationBar { |
|
|
|
/** |
|
Set the looks of the current navigation bar using the given `NavigationBarLooks`. |
|
|
|
- Parameter looks: The ´NavigationBarLooks` struct to apply to the current navigation bar. |
|
*/ |
|
func setBarLooks(looks: NavigationBarLooks) { |
|
backIndicatorImage = looks.backIndicatorImage |
|
backIndicatorTransitionMaskImage = looks.backIndicatatorTransitionImageMask |
|
barStyle = looks.barStyle ?? .Default |
|
barTintColor = looks.barTintColor |
|
shadowImage = looks.shadowImage |
|
tintColor = looks.tintColor |
|
translucent = looks.translucent ?? false |
|
|
|
looks.backgroundImageMetrics?.forEach { [unowned self] (metric, image) in |
|
self.setBackgroundImage(image, forBarMetrics: metric) |
|
} |
|
|
|
looks.titleVerticalPosition?.forEach { [unowned self] (metric, position) in |
|
self.setTitleVerticalPositionAdjustment(position ?? 0, forBarMetrics: metric) |
|
} |
|
|
|
titleTextAttributes = looks.titleTextAttributes |
|
} |
|
|
|
/** |
|
Fetch the current bar looks. |
|
|
|
- Returns: A `NavigationBarLooks` struct with values representing the current navigation bar. |
|
*/ |
|
func currentBarLooks() -> NavigationBarLooks { |
|
var backgroundImageMetrics = [UIBarMetrics: UIImage?]() |
|
var titlePositions = [UIBarMetrics: CGFloat]() |
|
metrics.forEach { |
|
backgroundImageMetrics[$0] = self.backgroundImageForBarMetrics($0) |
|
titlePositions[$0] = self.titleVerticalPositionAdjustmentForBarMetrics($0) |
|
} |
|
|
|
return NavigationBarLooks(backIndicatorImage: self.backIndicatorImage, |
|
backIndicatatorTransitionImageMask: self.backIndicatorTransitionMaskImage, |
|
barStyle: self.barStyle, |
|
barTintColor: self.barTintColor, |
|
shadowImage: self.shadowImage, |
|
tintColor: self.tintColor, |
|
translucent: self.translucent, |
|
backgroundImageMetrics: backgroundImageMetrics, |
|
titleVerticalPosition: titlePositions, |
|
titleTextAttributes: self.titleTextAttributes) |
|
} |
|
} |
Useful when one pushed view controller requires other navigation bar looks. Get current looks in
viewDidLoad
. Then inviewWillDissappear
reapply the previous looks.