首页 > 解决方案 > 应用程序转到背景一段时间后,CoreGraphic 的视图绘制消失了

问题描述

这是我的观点的代码。

override func draw(_ rect: CGRect) {

    let path = self.getPath()

    path.stroke()
}

func getPath() -> UIBezierPath {

    let path = UIBezierPath()

    path.lineWidth = 1
    path.lineCapStyle = .round
    path.lineJoinStyle = .round
    mainColor.setStroke()
    mainColor.setFill()
    path.move(to: CGPoint(x:0,y:0))

    path.addQuadCurve(to: endPointLeft, controlPoint: controlPointLeft)
    path.addLine(to: startPointRight)
    path.addQuadCurve(to: endPointRight, controlPoint: controlPointRight)
    path.addLine(to: startPointLeft)

    path.fill()
    path.stroke()

    return path
}

它工作正常。但有时进入后台一段时间后,这个视图就会消失。

***** 更多信息 *****

在 ViewController 中具有滚动更改此视图的高度的功能。更改高度约束后,调用self.view.layoutIfNeeded().

标签: iosswiftcore-graphics

解决方案


您可以做的是在应用程序确实为第一个添加观察者激活时重新布局子视图,如下所示在您的控制器中

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
}

@objc func applicationDidBecomeActive(_ notification:Notification)
{
    self.view.layoutIfNeeded()
}

override func viewWillDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
}

推荐阅读