简体   繁体   English

在UIAlertController中删除textField周围的填充和边框

[英]Remove padding and border around textField in UIAlertController

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. 我试图在iOS 9.3中的Swift中的UIAlertController中实现一个文本字段,不知怎的,我正在填充文本字段周围的填充和顶部边框。

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: 我的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. constraintEqualToConstant函数仅在iOS 9.0中可用,如果要支持早于9.0的iOS版本,则需要在代码中添加检查。 This could be the one causing the issue. 这可能是导致问题的原因。

if #available(iOS 9.0, *) 
{
  tf.heightAnchor.constraintEqualToConstant(50).active = true
} else 
{
  // Fallback on earlier versions
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM