我正在try 使用样式为.actionSheet的默认AlertViewController.由于某些原因,alert 会导致constraint error.只要alert tController不是通过按钮触发(显示)的,整个视图上就没有约束错误.会不会这是一架bug of 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)
    }

如你所见,它是very basic.这就是为什么我对自己的奇怪行为感到非常困惑,因为这default implementation应该不会导致任何错误,对吧?

Output I get

不过,通过打破限制,alert 可以在所有尺寸的屏幕上正确显示,我将非常感谢得到的任何帮助.

推荐答案

这个错误并不严重,似乎是来自苹果的未修复错误.该约束在演示后以动画样式显示.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)

Ios相关问答推荐

在金属中设置纹理的UV坐标以编程方式变形纹理

如何在wiftUI中管理导航栈?

我应该使用哪个UIButton发送事件在SWIFT上刷卡?

如何在Android或iPhone上的原生react 中处理其他应用程序通知?

iOS应用启动时崩溃问题解决方法-NO_CRASH_STACK-DYLD 4个符号丢失

UIView 阴影不适用于自定义扩展

SwiftUI - 如何从主题感知命名 colored颜色 中获取正确的 RGB 值?

iOS按钮和navigationLink在List中相邻

Swift 图表:.chartYScale 似乎只适用于 100 的增量?

Flutter 错误:CocoaPods not installed or not in valid state.

每次运行后,Xcode 6 都会在 iOS8 模拟器中重命名我的应用程序目录.

无论我将构建版本或应用程序版本更改为什么,都会出现 ITEMS-4238冗余二进制上传错误

用 PC 调试 ipad safari

如何判断是否为 64 位构建了静态库?

iPhone 未连接.连接 iPhone 后 Xcode 将继续

关闭通过模态 segue 显示的视图

正确使用 Alamofire 的 URLRequestConvertible

UITextView 内容插图

ARC - __unsafe_unretained 的含义?

更新到 iOS 14 和 Xcode 12 后应用启动时间变慢