【问题标题】:Navigation title not showing on Watch OS (IDE: Xcode)Navigation title not showing on Watch OS (IDE: Xcode)
【发布时间】:2022-12-27 13:34:21
【问题描述】:

I'm coding according to SwiftUI tutorial fromApple. But the preview result on my end which was without navigation title was different fromApplethough the code was the same and .navigationTitle was working well on iPhone simulator. Anyone who could help finding out the reason? Will be appreciate!

import SwiftUI

struct LandmarkDetail: View {
    @EnvironmentObject var modelData: ModelData
    var landmark: Landmark

    var landmarkIndex: Int {
        modelData.landmarks.firstIndex(where: { $0.id == landmark.id })!
    }

    var body: some View {
        ScrollView {
            VStack {
                CircleImage(image: landmark.image.resizable())
                    .scaledToFit()

                Text(landmark.name)
                    .font(.headline)
                    .lineLimit(0)

                Toggle(isOn: $modelData.landmarks[landmarkIndex].isFavorite) {
                    Text("Favorite")
                }

                Divider()

                Text(landmark.park)
                    .font(.caption)
                    .bold()
                    .lineLimit(0)

                Text(landmark.state)
                    .font(.caption)

                Divider()

                MapView(coordinates: landmark.locationCoordinates)
                    .scaledToFit()
            }
            .padding(16)
        }
        .navigationTitle("Landmarks")
    }
}

struct LandmarkDetail_Previews: PreviewProvider {
    static var previews: some View {
        let modelData = ModelData()
        return Group {
            LandmarkDetail(landmark: modelData.landmarks[0])
                .environmentObject(modelData)
                .previewDevice("Apple Watch Series 5 - 44mm")
        }
    }
}

【问题讨论】:

  • Put a NavigationView in the Preview area

标签: swift swiftui watchos


【解决方案1】:

I found that it works correctly on simulator after building the project without any code updating. But in preview mode, it's still wrong. I guessed it's a bug from Xcode... Not sure...

【讨论】:

    【解决方案2】:

    It still doesn't work in Xcode 14.1 and watchOS 9.1 either in the simulator, or a real device.

    struct ContentView: View {
    
    
    @State private var notes: [Note] = [Note]()
    @State private var text:String = ""
    
    
    
    // MARK: - BODY
    
    var body: some View {
        VStack{
            HStack(alignment:.center,spacing: 6){
                TextField("Add New Note", text: $text)
                
                Button{
                    //action
                }label: {
                    Image(systemName: "plus.circle")
                        .font(.system(size: 52,weight: .bold))
                }
                .fixedSize()
                .buttonStyle(PlainButtonStyle())
                .foregroundColor(.accentColor)
            }
            Spacer()
        }
        .navigationTitle("Notes")
      }
    }
    

    Update:

    Using NavigationStack is solved the problem instead of NavigationView

     var body: some View{
            NavigationStack{
                VStack{
                    Text("Watch Planner")
                        .padding()
                    NavigationLink(destination: ProgContentView(viewModel:viewModel)){
                        Text("Program")
                            .padding(.leading,5)
                    }
                }
                .navigationTitle("Test")
            }
        }
    

    【讨论】:

    • I'm trying to find the way to add the title as well..
    猜你喜欢
    • 2022-11-09
    • 2022-12-01
    • 2022-12-26
    • 1970-01-01
    • 2015-04-18
    • 2022-12-02
    • 2022-12-01
    • 2022-12-27
    • 2022-12-27
    相关资源
    最近更新 更多