Deno
Deno

Reputation: 63

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:(.

Upvotes: 3

Views: 570

Answers (1)

Tom Shen
Tom Shen

Reputation: 2028

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
}

Upvotes: 2

Related Questions