简体   繁体   中英

SwiftUI on Xcode Playground vs Swift Playground - different outputs

I'm running this code on Xcode Playground and Swift Playground:

import SwiftUI
import PlaygroundSupport

struct ContentView: View {
    var body: some View {
        VStack{
            ZStack {
                Color.white
                Text("hello World")
                    .foregroundColor(.black)
                
            }
            .edgesIgnoringSafeArea(.vertical)
            Circle()
                .foregroundColor(.blue)
        }
    }
}

PlaygroundPage.current.setLiveView(ContentView())

Anybody knows why we get so different outputs from the same code?

Swift Playground 结果

Xcode Playground 结果

On the iPad, you are running in Dark Mode, and that is making the default background of the view black. Also, the iPad Swift Playgrounds provides a frame in which the view runs. This frame varies if you switch between portrait and landscape mode, but in either case it is providing a frame in which to draw.

In Playgrounds in Xcode, the view is just taking up a minimum of size.

You can get a similar look in Xcode by providing a .frame and using a Color.black background:

import SwiftUI
import PlaygroundSupport

struct ContentView: View {
    var body: some View {
        VStack{
            ZStack {
                Color.white
                Text("hello World")
                    .foregroundColor(.black)
                
            }
            .edgesIgnoringSafeArea(.vertical)
            Circle()
                .foregroundColor(.blue)
        }
        .frame(width: 500, height: 600)
        .background(Color.black)
    }
}

PlaygroundPage.current.setLiveView(ContentView())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM