gpt4 book ai didi

ios - View Controller 上的设备旋转期间崩溃

转载 作者:行者123 更新时间:2023-11-29 05:57:15 25 4
gpt4 key购买 nike

我有一个自定义图像,我将其设置为 View 上的角标(Badge)。在旋转时,我调用一个函数来更改该图像的主要约束。当设备旋转时,以下代码会崩溃。

这是什么原因以及如何解决?

@IBOutlet weak var badgeLeadingConstraints: NSLayoutConstraint!

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

coordinator.animate(alongsideTransition: { context in
// This is called during the animation
}, completion: { context in
self.updateBadgeConstraints()
})
}

func updateBadgeConstraints() {
// Crash on following line
// Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
self.badgeLeadingConstraints.constant = (UIScreen.main.bounds.size.width / 4) + 34
}

最佳答案

您的badgeLeadingConstraintsnil。也许,设备旋转后它会变成nil。尝试使用尺寸类别或只是创建一个新的约束。

关于ios - View Controller 上的设备旋转期间崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55029717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com