Home > Back-end >  SwiftUI navigationTitle appear on a wired position and has a layout warning
SwiftUI navigationTitle appear on a wired position and has a layout warning

Time:06-19

This is a subView of my project enter image description here and it appears at a very low position xcode also give me the layout warning

import SwiftUI

struct DetailedView: View {
    var referFood:ReferFood
    @StateObject var recentFood=RecentSearched()
    var body: some View {
        NavigationView{
                Form{
                    Section("名称")
                    {
                        Text("\(referFood.name)")
                    }
                    Section("保质期")
                    {
                        Text("\(referFood.qualityTime)")
                    }
                    Section("储藏方法")
                    {
                        Text("\(referFood.method)")
                    }
                }.navigationTitle("详细信息")
                .navigationBarTitleDisplayMode(.inline)
        }
    }
}

It build successfully however when I drag the slider

CodePudding user response:

NavigationView should be in RootView only. Example in documentation

https://developer.apple.com/documentation/swiftui/navigationlink

is shown below:

Destination View:

struct ColorDetail: View {
    var color: Color

    var body: some View {
        color.navigationTitle(color.description)
    }
}

RootView:

NavigationStack {
    List {
        NavigationLink("Mint") { ColorDetail(color: .mint) }
        NavigationLink("Pink") { ColorDetail(color: .pink) }
        NavigationLink("Teal") { ColorDetail(color: .teal) }
    }
    .navigationTitle("Colors")
}

ps: NavigationStack is new version of navigation view. NavigationView is deprecated on iOS16

  • Related