UIImage与UIAertField中的UITextField重叠

时间:2017-04-07 09:32:18

标签: ios swift xcode

我的代码是:

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重叠。我怎样才能确保它们整齐地堆叠在一起?

3 个答案:

答案 0 :(得分:0)

从我的角度来看,如果您想使用自定义用户界面创建UIAlertController,请尝试通过自己创建。或者,如果你想使用这样一个hacky解决方案:

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

答案 1 :(得分: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)

设置框架对我有用。

答案 2 :(得分: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 }
}