SwiftUI-navigationBarBackButton

时间:2019-07-10 05:31:25

标签: navigationbar swiftui

  1. 如何更改navigationBarBackButton中的SwiftUI颜色? (通过 默认为蓝色)
  2. 如何更改navigationBarBackButton中的SwiftUI文本?

这是我的代码(示例):

struct ExercisesList : View {
    var exercises : [Exercise]
    var body: some View {

        NavigationView {
            List(self.exercises.identified(by: \.number)) {exercise in
                NavigationLink(destination: ExerciseDetailView(exercise: exercise)) {
                    ExerciseRow(exercisE: exercise)
                }
            }
            .navigationBarTitle(Text("Exercises"))

        }
    }
}

默认情况下,“ ExerciseDetailView”中的navigationBarBackButton的文本为 Exercise ,颜色为 blue

我的问题是我该如何操纵这两个?

2 个答案:

答案 0 :(得分:0)

这是一个解决方案。希望它会有所帮助。这样,您还可以使用自定义UIColor。

struct ExercisesList : View {
    var exercises : [Exercise]

    var body: some View {

        UINavigationBar.appearance().tintColor = UIColor.red

        return  NavigationView {
            List(self.exercises.identified(by: \.number)) { exercise in
                NavigationLink(destination: ExerciseDetailView(exercise: exercise)) {
                    ExerciseRow(exercisE: exercise)
                }
            }
        }
        .navigationBarTitle(Text("Exercises"))


    }
}

答案 1 :(得分:0)

当前,尚无直接方法可用于此方法(XCode 11 beta 3)。 但是,您可以为此使用UINavigationBar方法,

请参见以下代码:


 struct ExercisesList : View {
     var exercises : [Exercise]

     init() {
       //navigationBarBackButton color will change//
         UINavigationBar.appearance().tintColor = .purple
       // you can also set backgroundColor//
         UINavigationBar.appearance().backgroundColor = .white
     }

     var body: some View {

         NavigationView {
             List(self.exercises.identified(by: \.number)) {exercise in
                 NavigationLink(destination: ExerciseDetailView(exercise: exercise)) {
                     ExerciseRow(exercisE: exercise)
                 }
             }
             .navigationBarTitle(Text("Exercises"))
         }
     }
 }