swiftuiswift-playground

Why does my playground liveView look like an iPad instead of an iPhone in Xcode 15.2?


As title.
I have few code want to show in the playground liveView.
I wish it looks like iPhone, but it's not.
How to change the liveView to iPhone simulator in Xcode 15.2.
Should I miss some Xcode setting?

Here is my code:

import SwiftUI
import PlaygroundSupport
import Combine

struct ContentView: View {
        
    var body: some View {
        NavigationView {
            
            
            List{
                NavigationLink(destination: EmptyView()) {
                    Text("Test 1")
                }
                NavigationLink(destination: EmptyView()) {
                    Text("Test 2")
                }
            }
            .navigationBarTitle("Hello World")
        }
            
    }
}

PlaygroundPage.current.liveView = UIHostingController(
    rootView: ContentView()
)

enter image description here


Solution

  • NavigationView’s “default style” for iPad/Mac is a Master/Detail setup vs a Stack setup.

    What you are seeing is the Navigation’s 3rd view, it becomes obvious if you add one line to your code.

    NavigationView {
            
            List{
                NavigationLink(destination: EmptyView()) {
                    Text("Test 1")
                }
                NavigationLink(destination: EmptyView()) {
                    Text("Test 2")
                }
            }
            .navigationBarTitle("Hello World")
    
            Text(“third view”) ///<<——- here
        }
    

    You can “solve” it by setting the navigation style.

    Swift: Why does the iPad/Mac app version look weird

    Or using the newer and much betterNavigationStack.

    https://developer.apple.com/documentation/swiftui/migrating-to-new-navigation-types