我正在尝试将 UIPickerView
放在 UIAlertView
中,但我似乎无法正确调整它的大小。这是我得到的:
这是我的代码:
let alertView = UIAlertController(title: "Select item from list", message: "", preferredStyle: UIAlertControllerStyle.alert)
let pickerView = UIPickerView(frame: CGRect(x: 0, y: 0, width: 250, height: 60))
pickerView.dataSource = self
pickerView.delegate = self
alertView.view.addSubview(pickerView)
let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
alertView.addAction(action)
parent.present(alertView, animated: true, completion: nil)
最佳答案
诀窍是:
- 使用多行消息为您的新 View 留出空间
显示警报 View 时调整新 View 的大小
let alertView = UIAlertController( title: "Select item from list", message: "\n\n\n\n\n\n\n\n\n", preferredStyle: .alert) let pickerView = UIPickerView(frame: CGRect(x: 0, y: 50, width: 260, height: 162)) pickerView.dataSource = self pickerView.delegate = self // comment this line to use white color pickerView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2) alertView.view.addSubview(pickerView) let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil) alertView.addAction(action) present(alertView, animated: true, completion: { _ in pickerView.frame.size.width = alertView.view.frame.size.width })
关于swift - 在 UIAlertView 中调整 UIPickerView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41361177/