[英]SwiftUI on Xcode Playground vs Swift Playground - different outputs
我在 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 上,您在暗模式下运行,这使视图的默认背景为黑色。 此外,iPad Swift Playgrounds 提供了一个视图在其中运行的框架。 如果您在纵向和横向模式之间切换,此框架会有所不同,但在任何一种情况下,它都会提供一个用于绘制的框架。
在 Xcode 的 Playgrounds 中,视图只占用最小的尺寸。
您可以通过提供.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())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.