[英]SwiftUI on Xcode Playground vs Swift Playground - different outputs
I'm running this code on Xcode Playground and Swift Playground:我在 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())
Anybody knows why we get so different outputs from the same code?有谁知道为什么我们从相同的代码得到如此不同的输出?
On the iPad, you are running in Dark Mode, and that is making the default background of the view black.在 iPad 上,您在暗模式下运行,这使视图的默认背景为黑色。 Also, the iPad Swift Playgrounds provides a frame in which the view runs.
此外,iPad Swift Playgrounds 提供了一个视图在其中运行的框架。 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.在 Xcode 的 Playgrounds 中,视图只占用最小的尺寸。
You can get a similar look in Xcode by providing a .frame
and using a Color.black
background:您可以通过提供
.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.