Поместите UIPickerView в центр UIAlertController - PullRequest
0 голосов
/ 25 декабря 2018

Как я могу разместить UIPickerView в центре UIAlertController ?

Теперь у меня есть этот код:

let alertController = UIAlertController(title: "Представьтесь пожалуйста", message: "Кто вы?", preferredStyle: .alert)
alertController.isModalInPopover = true
userTypePickerView = UIPickerView(frame: CGRect(x: 0,
                                                y: 0,
                                                width: alertController.view.bounds.size.width,
                                                height: 140))
userTypePickerView.delegate = self
userTypePickerView.dataSource = self
let height: NSLayoutConstraint = NSLayoutConstraint(item: alertController.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 240)
alertController.view.addConstraint(height)
alertController.view.addSubview(userTypePickerView)

И ясм. эту картинку ...

enter image description here

1 Ответ

0 голосов
/ 25 декабря 2018

Несмотря на то, что я голосую за создание настраиваемого оповещения, но вы можете сделать это

let alertController = UIAlertController(title: "Представьтесь пожалуйста", message: "Кто вы?", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "ok", style: .default, handler: nil))
alertController.isModalInPopover = true
let userTypePickerView = UIPickerView(frame:.zero)
 alertController.view.addSubview(userTypePickerView)
userTypePickerView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    userTypePickerView.widthAnchor.constraint(equalToConstant: 200),
    userTypePickerView.heightAnchor.constraint(equalToConstant: 200),
    userTypePickerView.topAnchor.constraint(equalTo: alertController.view.topAnchor,constant:70),
    userTypePickerView.bottomAnchor.constraint(equalTo: alertController.view.bottomAnchor,constant:-40),
    userTypePickerView.centerXAnchor.constraint(equalTo: alertController.view.centerXAnchor)
])
userTypePickerView.backgroundColor = .red
self.present(alertController, animated: true, completion: nil)
...