在 Swift 游乐场中更改实时视图的框架

埃文·C

我不知道如何更改Live View frameSwift 游乐场中的大小这是我的代码:

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它变宽,改变它的大小。

反斜杠-f

您需要定义preferredContentSizeUIHostingController,如下所示:

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

结果

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章