[英]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.