将 UIPickerView 放在 UIAlertController 的中心
Place UIPickerView in the centre of the 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)
我看到这张照片...
尽管我投票赞成创建自定义提醒,但您可以这样做
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)
如何将 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)
我看到这张照片...
尽管我投票赞成创建自定义提醒,但您可以这样做
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)