在Swift游乐场中更改实时取景的框架

时间:2019-08-16 20:44:01

标签: swift swift-playground

我不知道如何在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变宽,更改其大小。

1 个答案:

答案 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
  

结果

enter image description here