【问题标题】:Add a tap gesture to SwiftUI navigationBarTitle?向 SwiftUI navigationBarTitle 添加点击手势?
【发布时间】:2020-01-28 18:21:52
【问题描述】:

我需要能够向导航栏标题添加点击手势识别器。但这似乎在 SwiftUI 中不支持?是否有任何解决方法,其他人是否设法做到这一点?

在相关说明中,是否不能将自定义视图显示为导航栏标题?现在我只能有一个文本视图,而不是一个图像(或带有手势修饰符的文本视图..)。

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    聚会很晚,但这是我找到的very good example,它适用于 iOS 14+。

    .toolbar {
            ToolbarItem(placement: .principal) {
                VStack {
                    Text("Title").font(.headline)
                    Text("Subtitle").font(.subheadline)
                }
            }
        }
    

    感谢这篇博文的作者,他帮我省了一些进一步的挖掘工作。

    【讨论】:

      【解决方案2】:

      它不是很优雅,但使用您的其中一个 navigationBarItems() 可以。

      struct ContentView: View {
          var body: some View {
              NavigationView{
                  Text("Hello World!")
                      .navigationBarItems(leading:YourView())
              }
          }
      }
      

      【讨论】:

      • 谢谢,但这取代了后退按钮,并且如果您将自己的后退按钮放在那里,也会使中心对齐变得混乱。对我来说不是一个很好的解决方案:(
      【解决方案3】:

      我喜欢这个

      struct TestView: View {
       @State static var title = "Some title text"
       @State static var profileImage = "person.crop"
       @State static var action : Int = 0
       ... 
       var body: some View {
          NavigationView {
          ...
      
      
          .navigationBarItems(leading: TitleLabel(title: TestView.$title), trailing: ProfileButton(imageName: TestView.$profileImage, action: TestView.$action))
          .navigationBarTitle(Text(""), displayMode: .inline)
         }
      }
      }
      

      struct TitleLabel: View {
      @Binding var title: String
      var body: some View {
          VStack {
              Text(self.title)
                  .font(.title)
                  .foregroundColor(Color.blue)
                  .fontWeight(.bold)
          }
          .padding(EdgeInsets.init(top: 0, leading: 0, bottom: 0, trailing: 0))
      }
      }
      
      struct ProfileButton: View {
      @Binding var imageName: String
      @Binding var action: Int
      @State private var showModal = false
      var body: some View {
          VStack {
              Button(action: {
      
              }) {
                  Image(systemName: imageName)
                      .resizable()
                      .imageScale(.large)
                      .frame(width: 36, height: 36, alignment: .center)
                      .padding(EdgeInsets.init(top: 0, leading: 0, bottom: 5, trailing: -20))
                      .foregroundColor(Color.black)
      
              }
          }
          .padding(.horizontal)
      }
      
      func doAction(action: Int) {
          switch action {
          case 0:
              print("open profile")
          default:
              print("Unkown action")
          }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-09-08
        • 1970-01-01
        • 2014-06-09
        • 1970-01-01
        • 1970-01-01
        • 2022-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多