【问题标题】:Why Today App Extension Widget in SwiftUI is white?为什么 SwiftUI 中的 Today App Extension Widget 是白色的?
【发布时间】:2020-07-29 10:49:23
【问题描述】:

just like this

我的观点很简单:

Text("Testing Widget")

并尝试过:

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

什么也没发生:(.

【问题讨论】:

  • 试图将背景颜色设置为清除?
  • 是的,我试过了,什么也没发生 :(
  • 如果你试试 .background(Color.white.opacity(0.0001))
  • .opacity(0.0001) 也没有用:((((
  • 提供更多代码,以便我们复制您的问题。

标签: ios swift widget swiftui extension-methods


【解决方案1】:

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

您必须将UIHostingController的view的backgroundColor设置为.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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 2014-07-24
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多