Skip to content

Instantly share code, notes, and snippets.

@simme
Last active August 15, 2017 07:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simme/7f98ac2334b92f2c48b89a1d47f03622 to your computer and use it in GitHub Desktop.
Save simme/7f98ac2334b92f2c48b89a1d47f03622 to your computer and use it in GitHub Desktop.
UINavigationBar extension for setting and applying "looks" in one go.
//
// 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)
}
}
@simme
Copy link
Author

simme commented Jul 19, 2016

Useful when one pushed view controller requires other navigation bar looks. Get current looks in viewDidLoad. Then in viewWillDissappear reapply the previous looks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment