繁体   English   中英

为什么在显示我的自定义 UIAlert 时会收到 Nil Layout Anchor 警告?

[英]Why am I getting Nil Layout Anchor warning while presenting my custom UIAlert?

我有一个自定义 UIAlert,我将内容视图控制器设置为我自己的自定义 VC。 警报功能正常,但我不断收到此错误:“约束工厂方法传递了一个 nil 布局锚点”。 我怀疑这与我添加子视图的方式有关,但我试图限制它们无济于事。 这是代码:

    let vc = UIViewController()
    vc.preferredContentSize = CGSize(width: 250,height: 150)
    let sortByPicker = UIPickerView(frame: CGRect(x: 0, y: 0, width: 250, height: 150))
    sortByPicker.tag = 1
    sortByPicker.delegate = self
    sortByPicker.dataSource = self

    vc.view.addSubview(sortByPicker)

    let editRadiusAlert = UIAlertController(title: "Sort Projects By...", message: "", preferredStyle: UIAlertController.Style.alert)

    editRadiusAlert.setValue(vc, forKey: "contentViewController")
    editRadiusAlert.addAction(UIAlertAction(title: "Done", style: .default, handler: nil))
    editRadiusAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    self.present(editRadiusAlert, animated: true)

这可能是因为您在将父视图控制器(警报控制器)添加到视图层次结构之前设置了contentViewController

您可能知道,如果您使用这个 hack, contentViewController是一个私有属性,您不应该访问/设置它。 如果您这样做,如果事情没有按预期工作或在未来突然中断,您不应该感到惊讶。

如果您真的想使用它,我建议您按照Leo Nathan 的回答并将UIAlertController子类UIAlertController 如果然后在viewDidLoad()分配contentViewController ,则应该正确设置所有内容,包括父级的布局锚点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM