我正在尝试在 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
}
关于ios - 删除 UIAlertController 中 textField 周围的填充和边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38648295/