2014-12-20 111 views
3

我想要一個簡單的帶圖像的UIAlertController。我該怎麼辦?在swift中將圖像添加到UIAlertController中

let alertMessage = UIAlertController(title: "Service Unavailable", message: "Please retry later", preferredStyle: .Alert) 
       alertMessage.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
       self.presentViewController(alertMessage, animated: true, completion: nil) 

回答

11

試試這個:

let alertMessage = UIAlertController(title: "Service Unavailable", message: "Please retry later", preferredStyle: .Alert) 

let image = UIImage(named: "myImage") 
var action = UIAlertAction(title: "OK", style: .Default, handler: nil) 
action.setValue(image, forKey: "image") 
alertMessage .addAction(action) 

self.presentViewController(alertMessage, animated: true, completion: nil) 
+3

不要再認爲這種方法有效 – chicobermuda

+0

這有效地將圖像添加到警報視圖的按鈕區域。這不是正確的方法。影響按鈕大小,並可能缺乏可訪問性。自定義提醒在缺少Apple提供的簡單方法的情況下更爲有效。 – user4806509

3

我不認爲有可能將圖像添加到UIAlertController。

-1

這是我的代碼圖像警報視圖

var logoAlert = UIImageView(frame: CGRectMake(0, 0, 300, 250)) 
    logoAlert.image = UIImage(named: "v1.png") 

    infoController.view.addSubview(logoAlert) 
    NSLog("image is draw") 

logoalert是dtring infoController - 是的樂趣 名字重新命名它,一切都會好起來的 粘貼此代碼在報警控制器

+0

歡迎來到StackOverflow!如果您編輯答案包含可讀,英文說明(而不是「dtring」,「好玩」),它會更容易理解和提高人民表決的機會。 :-) –

+0

我不知道你以前所獲得的選票下來,但你固定它和你的代碼爲我工作 –

0

的代碼片段,對我的作品的樂趣:

func alert2 (heading: String,image: String){ 
     if self.presentedViewController == nil { 
      let alertController = UIAlertController(title: nil,  message: " ", preferredStyle: .Alert) 
      var imageView = UIImageView(frame: CGRectMake(10, 6, 300, 50)) 
      imageView.image = UIImage(named: display.0) 
      alertController.view.addSubview(imageView) 
      alertController.addAction(UIAlertAction(title: "Maps", style: UIAlertActionStyle.Default, handler: {(action:UIAlertAction!) in self.makeContact("maps")})) 
相關問題