如何在Swift 4中更改UIAlertController的宽度?

时间:2018-09-10 15:15:58

标签: swift uialertcontroller

这是更改AlertController宽度的一种方法:

let alertController = UIAlertController(title: "Disconnected", message: "", preferredStyle: .alert)
let width : NSLayoutConstraint = NSLayoutConstraint(item: alertController.view, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 160) 
alertController.view.addConstraint(width)

但是运行时会出现错误消息:

    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:0x60000009ae50 UIView:0x7f9c4473cbf0.width == 270   (active)>",
    "<NSLayoutConstraint:0x6040002824e0 _UIAlertControllerView:0x7f9c460d4e00'Disconnected'.width >= UIView:0x7f9c4473cbf0.width   (active)>",
    "<NSLayoutConstraint:0x60c00008be00 _UIAlertControllerView:0x7f9c460d4e00'Disconnected'.width == 160   (active)>"

)

还有其他方法可以更改AlertController的宽度吗?还是有什么办法可以解决该错误?

任何建议将不胜感激。

0 个答案:

没有答案