如何在UIAlertController中添加UIPickerView?

问题描述 投票:-1回答:1

enter image description here

我创建了一个带有UIAlertControllerUITextfieldUIPickerview

当我按下按钮显示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)

    }
swift uipickerview uialertcontroller
1个回答
0
投票

您无法修改UIAlertController。而是,使普通的自定义呈现视图控制器。现在,界面由您决定。如果愿意,可以使显示的视图控制器的外观和行为类似于UIAlertController的视图(如我演示的here)。

© www.soinside.com 2019 - 2024. All rights reserved.