2015-09-26 23 views
6

Đây là cảnh báo của tôi hoạt động hoàn hảo. Tôi muốn thêm một hình ảnh vào cảnh báo xuất hiện dọc theo văn bản khi cảnh báo được trình bày và tôi đang ở trong SKScene trong SpriteKit nếu nó tạo sự khác biệt.Thêm hình ảnh vào 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) 

Trả lời

6

Bạn có thể thêm UIImageView làm tiểu sử cho UIAlertController.

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

Đây là cách bạn làm trong 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

Các phương pháp này không tuân thủ. Nó có hiệu quả thêm hình ảnh vào vùng nút và ảnh hưởng đến kích thước của các nút khác trong chế độ xem cảnh báo. Nó cũng có khả năng khả năng tiếp cận cho phương pháp này cũng không phù hợp. Apple nên đã làm cho nó có thể làm điều này ra khỏi hộp, nhưng không có tùy chọn đó, một cảnh báo tùy chỉnh được thực hiện từ một 'UIView' có vẻ như cách tiếp cận tốt nhất và có lợi ích của khả năng kiểm soát tất cả các yếu tố. – user4806509

3

Bạn có thể làm như thế này.

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) 

Swift 3

0

tôi khuyên bạn sử dụng thư viện này để tạo ra một hộp thoại tùy chỉnh thực sự dễ dàng:

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

Bạn chỉ cần có để tạo ra hình ảnh của bạn xem theo chương trình và thêm vào tiểu sử của nó:

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

Hy vọng nó sẽ giúp bạn đời!

Các vấn đề liên quan