[英]Why Today App Extension Widget in SwiftUI is white?
and my view is simply:我的观点很简单:
Text("Testing Widget")
and tried this:并尝试了这个:
VStack {
Text("Testing Widget")
}
.background(Color(UIColor.clear))
and nothing happend:(.什么也没发生:(。
Just came across a solution for me!刚刚为我找到了一个解决方案!
You have to set the backgroundColor
of the UIHostingController
's view
to .clear
, and then it will work.您必须将UIHostingController
view
的backgroundColor
设置为.clear
,然后它才能工作。 You don't need to set the background color of the SwiftUI view.您不需要设置 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.