I'm attempting to implement a textfield inside a UIAlertController in Swift in iOS 9.3, somehow I'm getting padding and a top border around the textfield.
Please see the below screenshot, I've added a thick border around the textfield to highlight the padding and top border/line.
The code for my UIAlertController:
func handleLoginForgotPassword() {
// create alert controller
let alertController = UIAlertController(title: "Password Reset", message: "\nEnter your email below and press Reset to reset your password.\n", preferredStyle: .Alert)
// create text field for email
alertController.addTextFieldWithConfigurationHandler { textField -> Void in
let tf = textField
tf.placeholder = "Email Address"
tf.autocorrectionType = .No
tf.autocapitalizationType = .None
tf.backgroundColor = UIColor.whiteColor()
tf.layer.borderWidth = 4.0
tf.heightAnchor.constraintEqualToConstant(50).active = true
// pull email from emailTextField if it exists
tf.text = self.emailTextField.text
}
// create "OK" alert action
let actionReset = UIAlertAction(title: "Reset", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("YES Pressed")
// do something
return
}
// create "Cancel" alert action
let actionCancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("NO Pressed")
// do something
return
}
// add the actions
alertController.addAction(actionReset)
alertController.addAction(actionCancel)
// present the controller
self.presentViewController(alertController, animated: true, completion: nil)
}
This behaviour seems odd and I can't find it referenced when searching for a similar issue.
The constraintEqualToConstant function is only available from iOS 9.0, you need to add checking to your code if you want to support iOS version older than 9.0. This could be the one causing the issue.
if #available(iOS 9.0, *)
{
tf.heightAnchor.constraintEqualToConstant(50).active = true
} else
{
// Fallback on earlier versions
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.