我在 Xcode Playground 和 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())
有人知道为什么我们从相同的代码得到如此不同的输出吗?
最佳答案
在 iPad 上,您在深色模式下运行,这会使 View 的默认背景变为黑色。此外,iPad Swift Playgrounds 提供了一个框架, View 在其中运行。如果您在纵向和横向模式之间切换,此框架会有所不同,但无论哪种情况,它都会提供一个框架供您绘制。
在 Xcode 的 Playgrounds 中, View 仅占用最小尺寸。
您可以通过提供 .frame
并使用 Color.black
背景在 Xcode 中获得类似的外观:
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())
关于Xcode Playground 上的 SwiftUI 与 Swift Playground - 不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64470811/