raj kumar raj kumar - 17 days ago 7
iOS Question

How to make alert view appear in center of the view controller using swift 2.2

I am developing a app using swift 2.2, in my app I am having a button when a user taps on that button alert view will appear which contains four buttons in vertical everything works fine but alert view appearing in bottom of the view controller how to display in center of the view controller.

Note:I don't want to display any title and message just i have to show four buttons only.

If I use this one:

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


It helps to display in center of the view controller but without title and message it looks not better so I used following code

Code:

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)

Answer

Just set the title and the message to nil. I tested it in swift 3.

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