如何使用swift 2.2在视图控制器的中心显示警报视图

时间:2016-11-21 08:07:47

标签: ios swift swift2 alertview

我正在开发一个使用swift 2.2的应用程序,在我的应用程序中我有一个按钮,当用户点击该按钮警报视图将出现其中包含垂直四个按钮一切正常但警报视图出现在视图控制器的底部如何在视图控制器的中心显示。

注意:我不想显示任何标题和消息,只需要显示四个按钮。

如果我使用这个:

let alert = UIAlertController(title: " ", message: "select colors", preferredStyle: UIAlertControllerStyle.Alert)

它有助于显示在视图控制器的中心但没有标题和消息它看起来不是更好所以我使用下面的代码

代码:

let alert = UIAlertController()

alert.addAction(UIAlertAction(title: "Red Color", style: UIAlertActionStyle.Default, handler: nil))

alert.addAction(UIAlertAction(title: "Blue Color", style: UIAlertActionStyle.Default, handler: nil))

alert.addAction(UIAlertAction(title: "Green Color", style: UIAlertActionStyle.Default, handler: nil))

alert.addAction(UIAlertAction(title: "Yellow Color", style: UIAlertActionStyle.Default, handler: nil))

self.presentViewController(alert, animated: true, completion: nil)

4 个答案:

答案 0 :(得分:0)

你可以这样做:

let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)

答案 1 :(得分:0)

它将自动出现在其属性的中心:)但你真正想要的是什么?

答案 2 :(得分:0)

在出示alert

之前,在代码中添加以下行
alert.popoverPresentationController!.sourceView = self.view
alert.popoverPresentationController!.sourceRect = self.view.bounds

答案 3 :(得分:0)

只需将标题和消息设置为nil即可。我在swift 3中测试了它。

let alert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.alert)