【问题标题】:UI freezes for a second when returning to the previous ViewController in Navigation Stack当返回到导航堆栈中的前一个 ViewController 时,UI 会冻结一秒钟
【发布时间】:2020-10-31 18:56:28
【问题描述】:

当我按下返回按钮时,我无法弄清楚是什么导致 UI 冻结了一秒钟。在我将背景图像添加到要转换的 viewController 之后,它就开始发生了。如果我只是使用“白色”作为背景颜色,则过渡不会冻结,它只会在我添加图像后冻结。

这是它的 gif 图像...

https://gfycat.com/waryagileichidna

这是我在 viewDidLoad 中调用以设置背景图像的扩展...

extension UIView {
    func addBackground(image:String) {
        self.backgroundColor = .white
        // screen width and height:
        let width = UIScreen.main.bounds.size.width
        let height = UIScreen.main.bounds.size.height
    
        let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
        imageViewBackground.image = UIImage(named: "\(image)")
        imageViewBackground.alpha = 0.5
        // you can change the content mode:
        imageViewBackground.contentMode = UIView.ContentMode.scaleAspectFill
        
        self.addSubview(imageViewBackground)
        self.sendSubviewToBack(imageViewBackground)
    }
}

【问题讨论】:

  • 这个方法的调用代码在哪里?
  • 某些东西需要在主线程上。请添加更多代码。
  • 尝试在主线程上添加过渡代码
  • 尝试使用 Instruments 中的时间分析器来查看花费最多的时间。

标签: ios swift uiviewcontroller uinavigationcontroller


【解决方案1】:

您能否尝试将以下行添加到您在viewDidLoad 中调用的addBackground 方法中。

imageViewBackground.clipsToBounds = true

我觉得这应该可以解决它。这看起来不像是冻结。

【讨论】:

    猜你喜欢
    • 2022-11-07
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 2023-02-14
    • 2013-07-23
    • 2013-04-29
    相关资源
    最近更新 更多