Xcode Playground 上的 SwiftUI 与 Swift Playground - 不同的输出

标签 swift xcode swiftui

我在 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())

有人知道为什么我们从相同的代码得到如此不同的输出吗?

Swift Playground results

Xcode Playground results

最佳答案

在 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/

相关文章:

ios - 可以在 SwiftUI 中实现 `ScrollView` 交互式键盘关闭吗?

swift - 如何更改标签中某些单词的颜色 - Swift 3

swift - Xcode/Firebase 改进搜索功能

xcode - 指针与地址不对齐

swiftui - 如何在不中断删除的情况下在 SwiftUI 中实现 TextField 列表

SwiftUI 发布的变量未更新

javascript - Firebase 注销用户所有 session

ios - AudioKit Swift 5 - 如何在启动/停止 AKFMOscillator 时停止尖叫声?

ios - 如何消除 “Leading and Trailing constraints … there is already a center constraint” 警告

ios - 如何刷新屏幕上的所有内容