IPickerView放在UIAlertController的中

IPickerView放在UIAlertController的中

如何将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)


我看这张照片

ios - 将UIPickerView放在UIAlertController的中心-LMLPHP

最佳答案

尽管我投票赞成创建自定义警报,但是您可以执行此操作

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)

关于ios - 将UIPickerView放在UIAlertController的中心,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53922916/

10-09 18:33