你如何在 Swift UI 中渲染 Text("Content") 在 Swift Playgrounds 中的 iPad 与 Xcode 在 Mac?

How do you render Text("Content") in SwiftUI on an iPad in Swift Playgrounds vs. Xcode on a Mac?

以上代码使用 Xcode 编辑器在 Mac 上运行。但是,相同的代码 returns: abort() called 在 iPad 上使用 Swift Playground 的编辑器。如何在 iPad?

上呈现 Text()

我按照此视频中的说明操作:

https://developer.apple.com/videos/play/wwdc2020/10643/

我在Apple的开发者论坛上发现了一个类似的问题,但没有真正的解决方案:

https://developer.apple.com/forums/thread/667357

我尝试减小大小,但没有帮助:

“我会假设基于 Abort() 的另一个问题,如果 Abort() 即将崩溃,它就会被调用,而且看起来视图没有获得实时屏幕的边界视图,所以它不知道 how/where 来渲染视图。"

import SwiftUI
import PlaygroundSupport

struct ProgressView: View {
   
   var body: some View {
       ZStack {
           Circle()
           .stroke(lineWidth: 40)
           .foregroundColor(.blue)
               Text("25%")
       }
   }
   
}

PlaygroundPage.current.setLiveView(ProgressView().padding(150))

这很可能是一个错误 — 我提交了反馈 #FB9092837。但在 Apple 修复它之前,添加一个硬编码的 .frame 可以作为一个 hacky 修复。

.frame(width: 500, height: 500)

但是,加载需要一段时间...它首先呈现在 top-left 角落,几秒钟后,移动到中心。

我发现如果将 ProgressView 放在另一个容器视图中,并在那里设置框架,速度会快得多。

代码如下:

import SwiftUI
import PlaygroundSupport

struct ProgressView: View {
    var body: some View {
        ZStack {
            Circle()
                .stroke(lineWidth: 40)
                .foregroundColor(.blue)
            Text("25%")
        }
    }
}

struct ContainerView: View {
    var body: some View {
        ProgressView()
            .frame(width: 500, height: 500)
    }
}

PlaygroundPage.current.setLiveView(ContainerView())