ios - 如何在 UIAlertController 中设置 UITextField 的高度?

标签 ios swift uitextfield uialertcontroller

<分区>

我知道要使用这段代码以编程方式设置常规文本字段的高度

textField.frame.size.height = 60

但是如果我在 UIAlertController 中将此代码实现到文本字段,它就不起作用。我还尝试将键盘更改为支持 ASCII,但在我运行该应用程序后它并没有更改键盘。看来我错过了什么

这是我的警报 Controller 的代码

let alertController = UIAlertController(title: "Write Comment", message: "Tell us why you are not satisfied", preferredStyle: .alert)

alertController.addAction(UIAlertAction(title: "Send", style: .default, handler: { alert -> Void in
    let textField = alertController.textFields![0] as UITextField
    textField.frame.size.height = 60
    textField.keyboardType = .asciiCapable

    let commentDefect = textField.text ?? ""
    self.sendComment(defectID: defectID, comment: commentDefect)

}))

alertController.view.tintColor = UIColor(red: 0/255, green: 129/255, blue: 58/255, alpha: 1)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
    textField.placeholder = ""
})

self.present(alertController, animated: true, completion: nil)

结果如下: enter image description here

高度好像还在17左右,这是哪里出了问题?

最佳答案

您已添加以下代码:-

alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
        textField.placeholder = ""
        let heightConstraint = NSLayoutConstraint(item: textField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 120)
        textField.addConstraint(heightConstraint)
}) 

希望对你有帮助。

重复

关于ios - 如何在 UIAlertController 中设置 UITextField 的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50386408/

相关文章:

iOS Cocoa Touch 添加对象到 NSArray

iphone - 如何捕获 iPhone 应用程序中的其他应用程序事件?

ios - 时区问题 "ZE8",如何将NSDate转换为NSString

swift - 当在 Swift Spritekit 的 level1Scene 中发生接触时,如何在我的 GameScene 中运行一个函数?

iOS Swift - 多个 UITextField 移动 UIView

ios - 从未知类(id)对象获取文本的更短方法

javascript - 检查 URL 方案是否已在 Cordova 的 iOS 或 Android 上注册

ios - 在 iPhone 设备上测试时 POST 请求超时

iOS 如何将键盘与文本字段结合起来并一起显示?

ios - 如何更改不同类中的变量? (iOS, swift )