Ios 通过打破iPad上的约束向UIAlertController结果添加视图

Ios 通过打破iPad上的约束向UIAlertController结果添加视图,ios,swift,autolayout,uialertcontroller,Ios,Swift,Autolayout,Uialertcontroller,Im在UIAlertController内部构建滑块,实际上它在iPhone上运行良好,但在iPad上出现了破坏约束的错误,在演示之前,我给出了高度140的警报作为约束 这是我的密码: let alertController = UIAlertController(title:"Title", message: "", preferredStyle: UIAlertControllerStyle.Alert) let slider = UISlider(frame: CGRectMake(35,

Im在
UIAlertController
内部构建滑块,实际上它在iPhone上运行良好,但在iPad上出现了破坏约束的错误,在演示之前,我给出了高度140的警报作为约束

这是我的密码:

let alertController = UIAlertController(title:"Title", message: "", preferredStyle: UIAlertControllerStyle.Alert)
let slider = UISlider(frame: CGRectMake(35, 50, 200, 20))
alertController.view.addSubview(slider)

let height:NSLayoutConstraint = NSLayoutConstraint(item: alertController.view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 140)
alertController.view.addConstraint(height);

alertController.addAction(UIAlertAction(title: "close", style: UIAlertActionStyle.Cancel, handler: { (error) -> Void in

}))

self.presentViewController(alertController, animated: true, completion: nil)
错误:

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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7b555900 V:[_UIAlertControllerView:0x788d2a60'Title'(140)]>",
    "<NSLayoutConstraint:0x789db3f0 V:[_UIAlertControllerView:0x788d2a60'Title'(1024)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x789db3f0 V:[_UIAlertControllerView:0x788d2a60'Title'(1024)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。

好吧,我有一个答案,但是你不会喜欢它的

如果执行以下操作(未添加约束):

您将获得以下信息:

Alert frame(0.0, 0.0, 768.0, 1024.0)
Alert frame(0.0, 0.0, 300.0, 85.5)
self.presentViewController(alertController, animated: true)
{
    let height:NSLayoutConstraint = NSLayoutConstraint(item: alertController.view,
                                            attribute: NSLayoutAttribute.Height,
                                            relatedBy: NSLayoutRelation.Equal,
                                            toItem: nil,
                                            attribute: NSLayoutAttribute.NotAnAttribute,
                                            multiplier: 1,
                                            constant: self.view.frame.height * 1.20)
    alertController.view.addConstraint(height);
这恰好是iPad Air在纵向模式下的宽度和高度。好的,这显然不是视图的宽度和高度。让我们稍微修改一下代码

self.presentViewController(alertController, animated: true)
{
    print("Alert frame" + String(alertController.view.frame))
}
这一次我得到了以下信息:

Alert frame(0.0, 0.0, 768.0, 1024.0)
Alert frame(0.0, 0.0, 300.0, 85.5)
self.presentViewController(alertController, animated: true)
{
    let height:NSLayoutConstraint = NSLayoutConstraint(item: alertController.view,
                                            attribute: NSLayoutAttribute.Height,
                                            relatedBy: NSLayoutRelation.Equal,
                                            toItem: nil,
                                            attribute: NSLayoutAttribute.NotAnAttribute,
                                            multiplier: 1,
                                            constant: self.view.frame.height * 1.20)
    alertController.view.addConstraint(height);
请注意,我的标题中有两个“\n\n”。所以,在展示之前,让我们假设他们正在做一些古怪的事情(记住苹果告诉我们这是一个不透明的类)

因此,让我们添加以下内容:

Alert frame(0.0, 0.0, 768.0, 1024.0)
Alert frame(0.0, 0.0, 300.0, 85.5)
self.presentViewController(alertController, animated: true)
{
    let height:NSLayoutConstraint = NSLayoutConstraint(item: alertController.view,
                                            attribute: NSLayoutAttribute.Height,
                                            relatedBy: NSLayoutRelation.Equal,
                                            toItem: nil,
                                            attribute: NSLayoutAttribute.NotAnAttribute,
                                            multiplier: 1,
                                            constant: self.view.frame.height * 1.20)
    alertController.view.addConstraint(height);
}

现在,祈祷好运,看看会发生什么

Unable to simultaneously satisfy constraints.
对我来说,这表明苹果在iPad上有限制,而在iPhone上没有。当我们抛出高度约束时,我们设置了一种情况,内部约束代码试图解决两个冲突的命令。因此,它只能通过打破限制来做到这一点

我用的是。行动表,但我也遇到了同样的问题。苹果似乎没有对iPhone使用约束。如果你没有建立一个通用的应用程序,那么你应该没事

然而,这一点很重要,苹果将来可能会对iPhone施加限制,这可能会导致同样的问题

底线是:

UIAlertController类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不能修改

对我来说,苹果应该更明确地说,“我们根本不支持修改”
。我怀疑他们认为“按原样”涵盖了这一点,但正如你可能已经看到的(正如我所看到的),许多人并没有测试所有情况

因此,我计划寻找一个同时支持iPhone和iPad的第三方版本(请参阅,这是一个类似的问题)


把我的时间浪费在一些可能会在iOS临时版本中出现问题的东西上是不值得的。

我自己也遇到了同样的问题——几乎就好像说“应该按原样使用”的文档是最后一句话。在这两种平台上都有效的唯一解决方案是放置“\n\n\n”在标题中。我将查看它^ ^我正在提交一份错误报告,苹果的文档确实需要澄清,不应该(如零)尝试修改UIAlertController(无论是否子类化)。这包括但不限于添加约束等。我想它应该是子类化的,你可以想象一个scrollView,你需要在其中添加一些东西,而不需要自定义它的none,因为要使用它。我想这没有什么错!!UIAlertController不支持每个Apple文档的子类化-理想情况下,你应该让自己的n类的外观。但是你不能使用UIAlertController作为基类。嗯,事实上你是对的,我已经在苹果的文档中检查过了。添加我自己的类会更容易。