为什么SwiftUI中的今天的App Extension Widget是白色的?

问题描述 投票:2回答:1

just like this

我的观点很简单:

Text("Testing Widget")

并尝试过:

VStack {
    Text("Testing Widget")
}
.background(Color(UIColor.clear))

没有任何反应:(。

ios swift widget swiftui extension-methods
1个回答
1
投票

刚刚为我找到了解决方案!

您必须将backgroundColorUIHostingControllerview设置为.clear,然后它将起作用。您无需设置SwiftUI视图的背景颜色。

let hostingController = UIHostingController(rootView: SwiftUIView())

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    view.addSubview(hostingController.view)
    addChild(hostingController)
    hostingController.didMove(toParent: self)

    // You may want to add constraints

    hostingController.view.backgroundColor = .clear // <- IMPORTANT
}
© www.soinside.com 2019 - 2024. All rights reserved.