Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

ios - SwiftUI: unwanted split view on iPad

Problem: a view on Pad shows up with unwanted split view.

My current setup is: Catalina OSX beta 5 + Xcode 11 Beta 5

Here is the code I used, with a Navigation View and a Navigation Title:

import SwiftUI

struct SwiftUIView: View {
    var body: some View {
        NavigationView {
            Text("Search")
                .navigationBarTitle(Text("Search"))
        }
    }
}

#if DEBUG
struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}
#endif

When simulated on iPad (both physical device and preview) instead of a full screen view, I get this split screen view:

Unwanted split view with NavigationView

If I have just a view, with no NavigationView, I get a full screen view:

import SwiftUI

struct SwiftUIView: View {
    var body: some View {
        Text("Hello World!")
    }
}

#if DEBUG
struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}
#endif

Full screen, but without NavigationView

How can I make a NavigationView full screen (not split screen) on iPad?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can apply the .navigationViewStyle(StackNavigationViewStyle()) modifier to the NavigationView!

... 
    NavigationView {
        Text("Hello world!")
    }
    .navigationViewStyle(StackNavigationViewStyle())
...

Edit: Below, I am answering Alexandre's questions from his comment:

  • Why full view is not the default for iPad? That's just a choice made by Apple...

  • Why this modifier goes outside of NavigationView closure, while the Navigation Title goes inside... Maybe this gives clarification: https://stackoverflow.com/a/57400873/11432719


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...