Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Today App Extension Widget in SwiftUI is white?

just like this

and my view is simply:

Text("Testing Widget")

and tried this:

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

and nothing happend:(.

like image 282
Deno Avatar asked Nov 18 '25 09:11

Deno


1 Answers

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. You don't need to set the background color of the SwiftUI view.

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
}
like image 100
Tom Shen Avatar answered Nov 20 '25 23:11

Tom Shen