Swift默认AlertViewController打破了约束

问题描述 投票:13回答:1

我试图使用样式.actionSheet的默认AlertViewController。由于某种原因,警报会导致约束错误。只要没有通过按钮触发(显示)alertController,整个视图就不会出现约束错误。难道这是Xcode的错误吗?

我得到的确切错误如下:

2019-04-12 15:33:29.584076+0200 Appname[4688:39368] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x6000025a1e50 UIView:0x7f88fcf6ce60.width == - 16   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000025a1e50 UIView:0x7f88fcf6ce60.width == - 16   (active)>

这是我使用的代码:

@objc func changeProfileImageTapped(){
        print("ChangeProfileImageButton tapped!")
        let alert = UIAlertController(title: "Change your profile image", message: nil, preferredStyle: .actionSheet)

        alert.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: nil))
        alert.addAction(UIAlertAction(title: "Online Stock Library", style: .default, handler: nil))
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        alert.view.tintColor = ColorCodes.logoPrimaryColor

        self.present(alert, animated: true)
    }

如你所见,这是非常基本的。这就是为什么我对我得到的奇怪行为感到非常困惑,因为这个默认实现不应该导致任何错误,对吧?

Output I get

虽然通过打破限制,警报会在所有屏幕尺寸上正确显示,但我会非常感谢我得到的任何帮助。

ios swift iphone xcode ios-autolayout
1个回答
15
投票

这个错误并不重要,苹果似乎是不固定的错误形式。刚出现后,此约束以动画样式显示。 enter image description here我试图在呈现之前捕捉并改变它(改变值,关系,优先级) - 由于这种动态添加的约束没有成功。

当您在self.present(alert, animated: false)中关闭动画并使用alert.view.addSubview(UIView())时 - 错误消失。我无法解释,但它的确有效!

let alert = UIAlertController(title: "Change your profile image", message: nil, preferredStyle: .actionSheet)

alert.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Online Stock Library", style: .default, handler: nil))
let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)

alert.addAction(cancel)
alert.view.addSubview(UIView()) // I can't explain it, but it works!

self.present(alert, animated: false)
© www.soinside.com 2019 - 2024. All rights reserved.