Skip to content

Instantly share code, notes, and snippets.

@khorbushko
Last active November 24, 2020 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khorbushko/c38db74d8c801336bb9d605f77bac62b to your computer and use it in GitHub Desktop.
Save khorbushko/c38db74d8c801336bb9d605f77bac62b to your computer and use it in GitHub Desktop.
NavigationBar - change backgroundColor
extension View {
func configureNavigationBar(_ configure: @escaping (UINavigationBar) -> ()) -> some View {
modifier(NavigationConfigurationViewModifier(configure: configure))
}
}
struct NavigationConfigurationViewModifier: ViewModifier {
let configure: (UINavigationBar) -> ()
func body(content: Content) -> some View {
content.background(NavigationControllerLayout(configure: {
configure($0.navigationBar)
}))
}
}
struct NavigationControllerLayout: UIViewControllerRepresentable {
var configure: (UINavigationController) -> () = { _ in }
func makeUIViewController(
context: UIViewControllerRepresentableContext<NavigationControllerLayout>
) -> UIViewController {
UIViewController()
}
func updateUIViewController(
_ uiViewController: UIViewController,
context: UIViewControllerRepresentableContext<NavigationControllerLayout>
) {
if let navigationContoller = uiViewController.navigationController {
configure(navigationContoller)
}
}
}
import Foundation
import UIKit
extension UINavigationBar {
enum Appearence {
case transparent
case defaultLight
case colored(UIColor?)
var color: UIColor {
switch self {
case .colored(let color):
return color ?? .clear
case .defaultLight:
return .white
case .transparent:
return .clear
}
}
var appearenceColor: UIColor {
switch self {
case .defaultLight:
return .white
case .transparent,
.colored:
return .clear
}
}
var tint: UIColor {
switch self {
case .colored(let color):
return color ?? .clear
case .defaultLight:
return .white
case .transparent:
return .clear
}
}
var effect: UIBlurEffect? {
switch self {
case .defaultLight:
return .init(style: .light)
case .transparent,
.colored:
return .none
}
}
}
func switchToAppearence(_ type: Appearence) {
backgroundColor = type.color
barTintColor = type.tint
standardAppearance.backgroundColor = type.appearenceColor
standardAppearance.backgroundEffect = type.effect
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment