[英]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.