Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public extension View {
- func navigationAppearance1(standardAppearance: NavigationBarAppearance,
- scrollAppearance: NavigationBarAppearance,
- currentAppearance: Binding<NavigationBarAppearance>) -> some View {
- modifier(NavigationAppearanceViewModifier1(standardAppearance: standardAppearance,
- scrollAppearance: scrollAppearance,
- currentAppearance: currentAppearance))
- }
- }
- private struct NavigationAppearanceViewModifier1: ViewModifier {
- let standardAppearance: NavigationBarAppearance
- let scrollAppearance: NavigationBarAppearance
- @Binding var currentAppearance: NavigationBarAppearance
- func body(content: Content) -> some View {
- content.background(NavigationAppearanceRepresentable1(standardAppearance: standardAppearance,
- scrollAppearance: scrollAppearance,
- currentAppearance: $currentAppearance))
- }
- }
- private struct NavigationAppearanceRepresentable1: UIViewControllerRepresentable {
- let standardAppearance: NavigationBarAppearance
- let scrollAppearance: NavigationBarAppearance
- @Binding var currentAppearance: NavigationBarAppearance
- func makeUIViewController(
- context: UIViewControllerRepresentableContext<NavigationAppearanceRepresentable1>
- ) -> NavigationAppearanceViewController1 {
- let viewController = NavigationAppearanceViewController1(standardAppearance: standardAppearance,
- scrollAppearance: scrollAppearance)
- viewController.onAppearanceChange = { appearance in
- currentAppearance = appearance
- }
- return viewController
- }
- func updateUIViewController(
- _ uiViewController: NavigationAppearanceViewController1,
- context: UIViewControllerRepresentableContext<NavigationAppearanceRepresentable1>
- ) {
- uiViewController.update(standardAppearance: standardAppearance, scrollAppearance: scrollAppearance)
- }
- }
- private final class NavigationAppearanceViewController1: UIViewController {
- let standardAppearance: NavigationBarAppearance
- let scrollAppearance: NavigationBarAppearance
- var onAppearanceChange: ((NavigationBarAppearance) -> Void)?
- init(standardAppearance: NavigationBarAppearance,
- scrollAppearance: NavigationBarAppearance) {
- self.standardAppearance = standardAppearance
- self.scrollAppearance = scrollAppearance
- super.init(nibName: nil, bundle: nil)
- }
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- if let navigationController = navigationController {
- navigationController.style(standardAppearance: standardAppearance, scrollAppearance: scrollAppearance)
- onAppearanceChange?(standardAppearance)
- }
- }
- func update(standardAppearance: NavigationBarAppearance,
- scrollAppearance: NavigationBarAppearance) {
- if let navigationController = navigationController {
- navigationController.style(standardAppearance: standardAppearance, scrollAppearance: scrollAppearance)
- onAppearanceChange?(scrollAppearance)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement