0

I know that to set the height of regular textfield programatically by using this code

textField.frame.size.height = 60

But if I implement this code to textfield inside my UIAlertController, it doesn't work. I also try to change the keyboard to ASCII capable, but it doesn't change the keyboard after I run the app. it seems I miss something

Here is the code of my alert 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)

and here is the result : enter image description here

It seems the height is still around 17. What went wrong here?

ItsPronounced
  • 5,475
  • 13
  • 47
  • 86
sarah
  • 3,819
  • 4
  • 38
  • 80
  • 1
    https://stackoverflow.com/questions/46171757/how-can-i-change-the-height-of-uitextfield-in-uialertcontroller-in-swift here is the answer – Wings May 17 '18 at 08:17

2 Answers2

3

you have add below code:-

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)
}) 

Hope it help you.

It's duplicate

V D Purohit
  • 1,179
  • 1
  • 10
  • 23
0
let alertController = UIAlertController(title: "Write Comment", message: "Tell us why you are not satisfied", preferredStyle: .alert)
        alertController.addTextField { (textField:UITextField) in

            let heightConstraint = NSLayoutConstraint(item: textField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: yourHeightValue)
            textField.placeholder = ""
            textField.addConstraint(heightConstraint)
            textField.keyboardType = .asciiCapable
        }
        alertController.addAction(UIAlertAction(title: "Send", style: .default, handler: { (alert) in

            let commentDefect = alertController.textFields?.first?.text ?? ""
            self.sendComment(defectID: defectID, comment: commentDefect)
        }))
        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        alertController.view.tintColor = UIColor(red: 0.0/255.0, green: 129.9/255.0, blue: 58.0/255.0, alpha: 1.0)
        self.present(alertController, animated: true, completion: nil)
Gaurav Patel
  • 532
  • 1
  • 5
  • 19