我想创建一个带有文本字段和选择器 View 的警报。 我希望首先在顶部显示选择器 View ,然后在选择器 View 下方显示文本字段。
这是我的代码
let alert = UIAlertController(title: "Create Meditation", message: "myMsg", preferredStyle: .alert)
alert.view.addSubview(pickerView)
alert.addTextField { (textField) in
textField.text = "Enter Message"
}
let action = UIAlertAction(title: "Send", style: .default) { (action) in
let textField = alert.textFields
print(textField)
}
let action2 = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(action)
alert.addAction(action2)
let height:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.50)
alert.view.addConstraint(height);
self.present(alert, animated: true, completion: nil)
最佳答案
您应该创建一个 uiviewcontroller 并赋予它这种效果。
例如:在下一张图片中,我创建了一个模态视图。它是一个具有灰色 View 的 uiviewcontroller (在您的情况下,您应该在灰色 View 中放置 uitextfield 和 uipickerview )。然后,我将 Storyboard Segue 配置为以模态方式呈现,并在 uiviewcontroller put 中给出模态的效果:
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.8)
关于swift3 - 在 UIAlertView 中设置 TextField 和 PickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43505011/