【问题标题】:SwiftUI: View flashes briefly white [duplicate]SwiftUI:视图短暂闪烁白色[重复]
【发布时间】:2020-10-14 22:44:33
【问题描述】:

我是一个初学者 SwiftUI 程序员,我遇到了以下问题:

我有两个视图,一个加载视图(矩形)和一个网页视图(WKWebView)。

像这样使用这两个视图:

var body : some View {
    ZStack {
        WebView(self.webView)
        Rectangle()
            .foregroundColor(self.state.isLoading ? self.loadingColor : Color.clear)
    }
    .onAppear {
        // Initialize web view etc.
        // Sets self.state.isLoading to false after some time has passed
    }
}

有趣的是,当self.state.isLoadingtrue 更改为false 时,UI 会短暂闪烁白色。

我不知道为什么会这样,特别是因为 WebView 在显示时肯定不是白色的。

我尝试添加背景矩形并设置self.webViewself.webView.scrollView的backgroundColor:

var body : some View {
    ZStack {
        Rectangle()
            .foregroundColor(Color.red)
        WebView(self.webView)
        Rectangle()
            .foregroundColor(self.state.isLoading ? self.loadingColor : Color.clear)
    }
    .onAppear {
        // Initialize web view etc.
        self.webView.backgroundColor = UIColor.red
        self.webView.scrollView.backgroundColor = UIColor.red
        // Sets self.state.isLoading to false after some time has passed
    }
}

我预计会看到一个短暂的红屏,但它仍然是白色的。

在 Rectangle() 过渡中添加动画使闪烁消失

Rectangle()
    .foregroundColor(self.state.isLoading ? self.loadingColor : Color.clear)
    .animation(.easeIn)

我是否遗漏了什么或做错了什么?

【问题讨论】:

    标签: swift swiftui uikit


    【解决方案1】:

    我认为这可能与 self.state.isLoading 变量有关,当它发生变化时,整个视图正在重新加载。您可以尝试将 isLoading 设置为结构中的局部变量 (@State var isLoading: Bool = false),然后改为切换局部变量吗?

    【讨论】:

      猜你喜欢
      • 2019-04-02
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多