Swift 默认的 AlertViewController 打破约束 [英] Swift default AlertViewController breaking constraints

查看:33
本文介绍了Swift 默认的 AlertViewController 打破约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use a default AlertViewController with style .actionSheet. For some reason, the alert causes a constraint error. As long as the alertController is not triggered (displayed) through a button, there are no constraint errors on the whole view. Could it be that this is a bug of Xcode?

我得到的确切错误如下所示:

The exact error I get looks like this:

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)
    }

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

As you can see, it is very basic. That's why I am very confused about the strange behavior I get as this default implementation should not cause any errors, right?

尽管通过打破限制,警报可以在所有屏幕尺寸上正确显示,我还是非常感谢我得到的任何帮助.

Although, through breaking the constraints, the alert displays properly on all screen sizes I would be really thankful for any help I get.

推荐答案

此错误并不严重,似乎是 Apple 未修复的错误.此约束仅在呈现后以动画样式出现. 我试图在呈现之前捕捉并更改它(更改值、关系、优先级)——由于这种动态添加的约束,没有成功.

This error is not critical, seems to be unfixed bug form Apple. This constraint appears in animation style just after presenting. I tried to catch and change it (change values, relations, priority) before presenting – no success because of this dynamically added constraints.

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

When you turn off animation in self.present(alert, animated: false) and using alert.view.addSubview(UIView()) – the error disappears. I can't explain it, but it works!

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)

这篇关于Swift 默认的 AlertViewController 打破约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆