更改UIAlertController文本字段的背景颜色不起作用

时间:2017-01-03 03:30:20

标签: ios swift uikit

let alert = UIAlertController(title: "Breaking Point Stack", message: "What's the average breaking point stack you have?", preferredStyle: UIAlertControllerStyle.alert)

alert.addTextField() { textField in
    textField.backgroundColor = UIColor.clear
    textField.useUnderLine()
}

self.present(alert, animated: true, completion: nil)

文本字段预计在底部不显示背景颜色和白线,但是,当显示警报控制器时,文本字段的背景仍为白色,带有黑色边框。

以下是useUnderLine()的代码:

func useUnderLine() {
    self.borderStyle = .none
    self.layoutIfNeeded()

    let border = CALayer()
    let width = CGFloat(1.0)
    border.borderColor = UIColor.white.cgColor
    border.frame = CGRect(x: 0, y: self.frame.size.height - width, width:  self.frame.size.width, height: width)
    border.borderWidth = width

    self.layer.addSublayer(border)
    self.layer.masksToBounds = true
}

1 个答案:

答案 0 :(得分:1)

边境&如果您按照图1的视图层次调试它,则实际上UITextField不会决定背景样式。

View hierarchy

你做了这个改变:

    present(alert, animated: false, completion: {
        if let textField = alert.textFields?.first {
            textField.useUnderLine()
        }
    })

修改预备功能:

$user->newSubscription('main', $plan)->create($creditCardToken, ['email' =>$email,]);

Screenshot

但是请注意,View层次结构在未来可能会有不同的iOS版本。