DaisukeNagata / SwiftUI2.0

3 stars 0 forks source link

ViewModifier_navigationTitle #5

Open DaisukeNagata opened 4 years ago

DaisukeNagata commented 4 years ago
スクリーンショット 2020-06-28 11 07 41
DaisukeNagata commented 4 years ago
スクリーンショット 2020-06-28 11 08 38
DaisukeNagata commented 4 years ago
スクリーンショット 2020-06-28 11 09 25
DaisukeNagata commented 4 years ago
import SwiftUI

struct ContentView: View {

    @State var c = UIColor.green

    init() {
        let appearance = UINavigationBarAppearance()
        appearance.backgroundColor = c
        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance
    }

    var body: some View {
        NavigationView {
            VStack {
                self.modifier(NavigationModifier(st: "Modifier"))
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct NavigationModifier: ViewModifier {

    @State var st: String
    @State var flg = false
    @State var c = UIColor.white

    func body(content: Content) -> some View {
        let appearance = UINavigationBarAppearance()
        appearance.backgroundColor = self.c
        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance

        return Text(self.st)
            .navigationTitle("navigationTitle")
            .onTapGesture {
                self.flg.toggle()
            }
            .onChange(of: "\(self.flg)") { value in
                if value == "false" {
                    c = c == UIColor.green ? UIColor.orange : UIColor.green
                }
            }
            .frame(maxHeight:.infinity, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
            .navigationBarHidden(flg)
            .edgesIgnoringSafeArea(.all)
    }
}
DaisukeNagata commented 4 years ago
import SwiftUI

struct ContentView: View {

    var body: some View {
        self.modifier(NavigationModifier(st: "Modifier"))
    }
}

struct NavigationModifier: ViewModifier {

    @State var st: String
    @State var flg = false
    @State var c = UIColor.blue

    func body(content: Content) -> some View {
        let appearance = UINavigationBarAppearance()
        appearance.backgroundColor = self.c
        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance

        return
            NavigationView {
                Text(self.st)
                    .navigationTitle("navigationTitle")
                    .onTapGesture {
                        self.flg.toggle()
                    }
                    .onChange(of: self.flg) { value in
                        guard value else {
                            c = c == UIColor.green ? UIColor.orange : UIColor.green
                            return
                        }
                    }
                    .frame(maxHeight:.infinity, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                    .navigationBarHidden(flg)
                    .edgesIgnoringSafeArea(.all)
            }.background(Color.clear)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}