我创建了一个带有UIAlertController
和UITextfield
的UIPickerview
。
当我按下按钮显示UIAlert时,UIPickerView
已完全移位。我该如何解决?
这是我的代码:
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return passwordCategory.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
passwordCategory[row]
}
func passwordAlert() {
let alert = UIAlertController(title: "type in some text", message: "", preferredStyle: .alert)
alert.addTextField { (passwordCategoryTextfield) in
passwordCategoryTextfield.placeholder = "e.g Business"
}
let pickerFrame = CGRect(x: 0, y: 0, width: 250, height: 300)
let picker = UIPickerView(frame: pickerFrame)
picker.delegate = self
picker.dataSource = self
alert.view.addSubview(picker)
alert.addAction(UIAlertAction(title: "add", style: .default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
let passwordInfo = alert.textFields?.first?.text
self.individualPasswordInformaiton.insert("\(passwordInfo ?? "")", at: 0)
print(self.individualPasswordInformaiton)
}))
alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
您无法修改UIAlertController。而是,使普通的自定义呈现视图控制器。现在,界面由您决定。如果愿意,可以使显示的视图控制器的外观和行为类似于UIAlertController的视图(如我演示的here)。