2017-04-07 70 views
0

我的代碼中是這樣的:的UIImage用的UITextField重疊的UIAlertController

let alert = UIAlertController(title: "title", message: "alert", preferredStyle: UIAlertControllerStyle.Alert) 
let image_view = UIImageView(frame: CGRectMake(70, 10, 200, 200)) 
image_view.image = UIImage(named: "logo") 
alert.view.addSubview(image_view) 
alert.addTextFieldWithConfigurationHandler { textField in textField.placeholder = "a textfield" 
alert.addTextFieldWithConfigurationHandler { textField in textField.placeholder = "another textfield" } 

然而,這導致我與UITextFields標誌重疊。我如何確保它們整齊地堆疊在一起?

+0

您沒有設置文本框的框架來設計您的自定義報警控制器。 – Anuraj

回答

0

從我的角度來看,如果您想用自定義UI創建UIAlertController,請嘗試通過您自己創建它。或者,如果你想用有點哈克的解決方案是這樣的:

let image = UIImage(named: "logo") 
alert.setValue(image, forKey: "image") 
+0

我得到了一個'setValue:forUndefinedKey:]:這個類不是符合密鑰值的密鑰值, –

0

嘗試設置框架文本字段: -

var textField = UITextField()   
    textField.frame = CGRect(x: , y: image_view.bounds.minY + 10, width: 250, height: 40) 
    textField.placeholder = "a textfield" 
    alertController.view.addSubview(textField) 

設定框爲我工作。

0

你需要example 因爲UIAlertController限制了以下功能

@available(iOS 8.0, *) 
open class UIAlertController : UIViewController { 


    public convenience init(title: String?, message: String?, preferredStyle: UIAlertControllerStyle) 


    open func addAction(_ action: UIAlertAction) 

    open var actions: [UIAlertAction] { get } 


    @available(iOS 9.0, *) 
    open var preferredAction: UIAlertAction? 


    open func addTextField(configurationHandler: ((UITextField) -> Swift.Void)? = nil) 

    open var textFields: [UITextField]? { get } 


    open var title: String? 

    open var message: String? 


    open var preferredStyle: UIAlertControllerStyle { get } 
} 
相關問題