2017-04-19 79 views
0

我想創建一個帶有文本框和選擇器視圖的警報。 我希望在頂部顯示選擇器視圖,然後在選擇器視圖下方的文本框。在UIAlertView中設置TextField和PickerView

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) 

回答

0

不能使用UIAlertController爲,因爲它不會有一個選項,添加其他視圖比文本框。 所以,你必須編寫自己的控件,這將模仿UIAlertController。 您可以編寫視圖控制器並以模態方式呈現它,或創建視圖並將其添加到控制器視圖。 此外,嘗試在github.comcocoacontrols.com上找到現成的組件並根據需要進行修改可能是個好主意。

1

你應該創建一個uiviewcontroller並給它這個效果。

例如:在下一張圖片中,我創建了一個視圖爲模態。它是一個帶有灰色視圖的uiviewcontroller(在您的情況下,在灰色視圖中,您應該放置uitextfield和uipickerview)。然後,我配置的故事板賽格瑞作爲當前模態,並給予在該模式的效果的UIViewController地說:

self.view.backgroundColor = UIColor.black.withAlphaComponent(0.8) 

enter image description here

enter image description here

相關問題