swift3 - 在 UIAlertView 中设置 TextField 和 PickerView

标签 swift3 xcode8.2

我想创建一个带有文本字段和选择器 View 的警报。 我希望首先在顶部显示选择器 View ,然后在选择器 View 下方显示文本字段。

enter image description here

这是我的代码

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)

enter image description here

enter image description here

关于swift3 - 在 UIAlertView 中设置 TextField 和 PickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43505011/

相关文章:

ios - 使用 Firebase 和 Swift 3 检查用户登录状态并加载基于它的 View Controller

ios - 通过 segue 传递数据不能在 swift 3 中工作任何解决方案?

ios - 如何使 Xcode 8.2.1 刷新配置文件列表?看起来像 Xcode 错误

swift - Playground 时间轴不显示 View

ios - swift 中的 Core Image TileEffect 过滤器返回 nil 图像

ios - 我无法收到数据通知 iOS swift 3

swift - IBOutlet 为零

ios - 如何从方法中快速更新 UI

swift - 返回上一个 View Controller 时重新加载 Collection View

objective-c - swift 3 _ObjectiveCBridgeable 一致性