2016-11-21 48 views
0

我正在開發一個應用程序使用swift 2.2,在我的應用程序我有一個按鈕,當用戶點擊該按鈕時將出現警報視圖,其中包含四個按鈕在垂直一切工作正常,但警報視圖出現在視圖控制器的底部如何顯示在視圖控制器的中心。如何使警報視圖出現在視圖控制器的中心使用swift 2.2

注:我不想顯示任何標題和消息,只需要顯示四個按鈕。

如果我用這一個:

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

它有助於在視圖控制器的中心,但沒有title,消息顯示它看起來並不好,所以我用下面的代碼

代碼:

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) 

回答

0

只需設置標題和消息爲零。我在迅速3.

let alert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.alert) 
+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) 
+0

測試它我不想顯示任何標題和消息 –

+0

讓alertView = UNAlertView(標題: 「」,消息:「「) alertView.messageAlignment = NSTextAlignment.Center alertView。 buttonAlignment = UNButtonAlignment.Horizo​​ntal alertView.addButton( 「紅顏色」,動作:{ }) alertView.addButton( 「藍色」,動作:{ }) alertView.show() –

+0

你可以嘗試用上面的代碼... –

0

它會自動進入中心的屬性:)但你真正想要什麼?

+0

我會檢查並回復回復....謝謝 –

+0

https://pastebin.ubuntu.com/23260075/ – KhanShaheb

+0

沒有它出現在視圖控制器的底部 –

0

添加以下代碼行您出示您的alert之前,

alert.popoverPresentationController!.sourceView = self.view 
    alert.popoverPresentationController!.sourceRect = self.view.bounds 
+1

它顯示我意外發現的錯誤致命錯誤無解包裝可選值 –

+0

無法加載任何客觀的C類信息 –

相關問題