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

标签 ios swift swift2 uitextfield ios9

我正在尝试在 iOS 9.3 的 Swift 中的 UIAlertController 中实现一个文本字段,不知何故我在文本字段周围得到了填充和顶部边框。

请看下面的屏幕截图,我在文本字段周围添加了一个粗边框以突出显示填充和顶部边框/线条。

Screenshot

我的 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/

相关文章:

ios - 当没有 ScrollView 正在减速时,UIScrollView isDragging 返回 YES

ios - 最适合互动书籍的 Controller 是什么?

iphone - InApp 在数组中购买奇怪的排列

swift - 从外部引用设置 .attributedText 属性(可通过 NIB 加载的 UIView 内的 socket 访问)似乎失败

ios - 基于Notification有效负载快速显示Viewcontroller

xcode - 如何使用变量来构造命令

ios - 包含在 Swift 中放置在自己的方法中时语法不同的地方

xcode - 如何使用 key 在 Realm (swift 2) 中获取数据

arrays - Swift 2 Object Mapper 类追加不起作用,结果为零

ios - 从字符串初始化 NSItemProvider 的问题