我的观点很简单:
Text("Testing Widget")
并尝试过:
VStack {
Text("Testing Widget")
}
.background(Color(UIColor.clear))
没有任何反应:(。
刚刚为我找到了解决方案!
您必须将backgroundColor
的UIHostingController
的view
设置为.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
}