【问题标题】:Remove padding and border around textField in UIAlertController删除 UIAlertController 中 textField 周围的填充和边框
【发布时间】:2016-07-28 23:26:56
【问题描述】:

我正在尝试在 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)
}

这种行为看起来很奇怪,我在搜索类似问题时找不到它的引用。

【问题讨论】:

  • 您已将borderWith 设置为4.0,这就是您看到边框的原因。删除那条线,边框就会消失。
  • @ChristianAbella 该边框仅用于说明它周围的填充/边框问题。这里没有边界:http://i.imgur.com/5gckZVu.png
  • 您要删除哪个填充?上,左?
  • @ChristianAbella 文本字段周围的填充。我只是希望它本质上看起来像一个普通的文本字段。为什么这个文本字段的边缘有这个空白?以及该区域顶部的线条/边框?
  • 我尝试了您的代码,但删除了这两行并且填充消失了:tf.layer.borderWidth = 4.0 tf.heightAnchor.constraintEqualToConstant(50).active = true

标签: ios swift swift2 uitextfield ios9


【解决方案1】:

constraintEqualToConstant 函数仅适用于 iOS 9.0,如果您想支持低于 9.0 的 iOS 版本,您需要在代码中添加检查。这可能是导致问题的原因。

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

【讨论】:

  • 我正在将此部署到 iOS 9.3 设备,所以我认为这不是问题所在。感谢您的建议。
猜你喜欢
  • 1970-01-01
  • 2020-04-18
  • 2022-01-25
  • 1970-01-01
  • 2014-01-30
  • 2012-04-09
  • 2013-01-07
  • 1970-01-01
  • 2017-08-28
相关资源
最近更新 更多