-
UIAlertController是不可自定义的。您可以为这种情况创建自己的alertViewController,然后根据需要使用UI。
-
在您的addTextfield中,提供对self的委托
alert.addTextField { (textField) in
textField.frame = CGRect(x: 20,y: 100,width: 300,height: 80)
textField.placeholder = "Please enter message"
textField.borderStyle = UITextField.BorderStyle.line
textField.delegate = self
textField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
}
然后您可以使用shouldChangeCharactersIn方法来限制长度
func textField(_ textField: UITextField,shouldChangeCharactersIn range: NSRange,replacementString string: String) -> Bool {
let maxLimit = 180
let currentString = textField.text!
let newString: String =
currentString.replacingCharacters(in: range,with: string) as String
return newString.length <= maxLength
}
确保您的ViewController符合UITextFieldDelegate。
class YourViewController: UIViewController,UITextFieldDelegate {
}
,
alert.addTextField { textField in
let heightConstraint = NSLayoutConstraint(item: textField,attribute: .height,relatedBy: .equal,toItem: nil,attribute: .notAnAttribute,multiplier: 1,constant: 100)
textField.addConstraint(heightConstraint)
}
,
因此,经过我的一些研究,我设法实现了这一点:
extension ViewController: UITextViewDelegate {
func alertWithTF() {
let alert = UIAlertController(title: "Title",message: "\n\n\n\n\n",preferredStyle: .alert)
let textView = UITextView()
textView.borderColor = .black
textView.borderWidth = 1.0
textView.delegate = self
textView.text = Constants.textViewPlaceholder
textView.textColor = .lightGray
alert.view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.topAnchor.constraint(equalTo: alert.view.topAnchor,constant: 125).isActive = true
textView.rightAnchor.constraint(equalTo: alert.view.rightAnchor,constant: -20).isActive = true
textView.leftAnchor.constraint(equalTo: alert.view.leftAnchor,constant: 20).isActive = true
textView.bottomAnchor.constraint(equalTo: alert.view.bottomAnchor,constant: -60).isActive = true
textView.heightAnchor.constraint(equalToConstant: 60).isActive = true
alert.view.translatesAutoresizingMaskIntoConstraints = false
alert.addAction(UIAlertAction(title: "Cancel",style: .default,handler: { [weak self] (_) in
self?.dismiss(animated: true,completion: nil)
}))
alert.addAction(UIAlertAction(title: "OK",handler: { [weak self] (_) in
let message = (textView.text == nil||textView.text == Constants.textViewPlaceholder ? "" : textView.text)
print(message)
}))
self.present(alert,animated: true,completion: nil)
}
所以我创建了一个TextView而不是TextField,并在alertView中为其设置了约束。这些代码不是完美的,但是效果很好。我也使用TextView委托来处理textView字符限制a和占位符,因为UITextView不支持它:
func textView(_ textView: UITextView,shouldChangeTextIn range: NSRange,replacementText text: String) -> Bool {
let newText = (textView.text as NSString).replacingCharacters(in: range,with: text)
let numberOfChars = newText.count
return numberOfChars < 101 //Max 100 chars
}
func textViewDidBeginEditing(_ textView: UITextView) {
if textView.textColor == UIColor.lightGray {
textView.text = ""
textView.textColor = UIColor.black
}
}
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text == "" {
textView.text = Constants.textViewPlaceholder
textView.textColor = UIColor.lightGray
}
}
希望这会有所帮助!
本文链接:https://www.f2er.com/2963902.html