我不知道如何在Swift游乐场中更改Live View frame
的大小。这是我的代码:
public struct PlaygroundRootView: View {
public var body: some View {
Rectangle()
.frame(width: 200, height: 100, alignment: .center)
.foregroundColor(Color.blue.opacity(0.7))
.cornerRadius(20)
}
}
PlaygroundPage.current.liveView = UIHostingController(rootView: PlaygroundRootView())
我想将Live View
变宽,更改其大小。
答案 0 :(得分:0)
您需要定义preferredContentSize
中的UIHostingController
,如下所示:
import PlaygroundSupport
import SwiftUI
public struct PlaygroundRootView: View {
public var body: some View {
Rectangle()
.frame(width: 200, height: 100, alignment: .center)
.foregroundColor(Color.blue.opacity(0.7))
.cornerRadius(20)
}
}
let host = UIHostingController(rootView: PlaygroundRootView())
host.preferredContentSize = CGSize(width: 600, height: 600)
PlaygroundPage.current.liveView = host
结果