[英]Remove padding and border around textField in UIAlertController
我试图在iOS 9.3中的Swift中的UIAlertController中实现一个文本字段,不知怎的,我正在填充文本字段周围的填充和顶部边框。
请看下面的截图,我在文本字段周围添加了一个粗边框,以突出显示填充和顶部边框/线条。
我的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)
}
这种行为似乎很奇怪,我在搜索类似问题时找不到它。
constraintEqualToConstant函数仅在iOS 9.0中可用,如果要支持早于9.0的iOS版本,则需要在代码中添加检查。 这可能是导致问题的原因。
if #available(iOS 9.0, *)
{
tf.heightAnchor.constraintEqualToConstant(50).active = true
} else
{
// Fallback on earlier versions
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.