2015-09-26 7 views
6

これは完全に機能する私の警告です。アラートが表示されたときにテキストの横に表示されるアラートに画像を追加したいのですが、違いがある場合はSpriteKitのSKSceneにあります。UIAlertControllerに画像を追加する

var alertController = UIAlertController(title: "How to Skate", message: "Tap the screen to perform a trick and jump over the obsticles (You can grind on rails) The game will end when you hit a highlighted red, orange or yellow obsticle. Thats it! + Image", preferredStyle: UIAlertControllerStyle.Alert)  
alertController.addAction(UIAlertAction(title: "Cool!", style: UIAlertActionStyle.Cancel, handler: nil)) 
self.view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil) 

答えて

6

あなたのUIAlertControllerにサブビューとしてUIImageViewを追加することができます。

var imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40)) 
imageView.image = yourImage 
alert.view.addSubview(imageView) 

これは、あなたがUIAlertControllerで行う方法です。

let alertMessage = UIAlertController(title: "My Title", message: "My Message", 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) 
+1

これらのアプローチは準拠していません。効果的にボタン領域に画像を追加し、アラートビューの他のボタンのサイズに影響を与えます。また、このアプローチのアクセシビリティも準拠していない可能性があります。 Appleはこれを可能にしたはずだが、そのオプションがなければ、「UIView」からのカスタムアラートが最良のアプローチのように思え、すべての要素を制御できるという利点がある。 – user4806509

3

あなたはこのように行うことができます。

let imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40)) 
    // imageView.image = UIImage(named: "ic_no_data") 
    let alertMessage = UIAlertController(title: "My Title", message: "", preferredStyle: .Alert) 

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

    self.presentViewController(alertMessage, animated: true, completion: nil) 
    alertMessage.view.addSubview(imageView) 
3
let alertMessage = UIAlertController(title: "My Title", message: "", preferredStyle: .alert) 
    let action = UIAlertAction(title: "OK", style: .default, handler: nil) 
    action.setValue(UIImage(named: "task1.png")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image") 
    alertMessage .addAction(action) 
    self.present(alertMessage, animated: true, completion: nil) 

スウィフト3

0

私は非常に本当に簡単なカスタムダイアログを作成するために、このライブラリを使用することをお勧めいたします:

https://github.com/vikmeup/SCLAlertView-Swift

あなたは自分の画像を作成する必要がありますプログラムで表示し、そのサブビューに追加:

let imageView = UIIMageView(frame: CGRectMake(x,10,180,25)) 
imageView.image = UIImage(named:"image.jpg") 
imageView.layer.borderColor = UIColor.greenColor().CGColor 
subview.addSubview(imageView) 

仲間に役立つことを願っています!