swift 删除 TextField 边框
swift remove TextField borders
我在警报中有一个文本字段。
我已经像这样配置了它的 borderStyle:
textField.borderStyle = .roundedRect
但是如您所见,第一个边框周围还有另一个矩形边框:
我想通过代码将其删除,但找不到任何选项或操作方法。
这是警报代码:
private func presentUsernameAlert() {
let alert = UIAlertController(title: nil, message: "Alors ?", preferredStyle: .alert)
alert.addTextField(configurationHandler: newUsername)
alert.addAction(UIAlertAction(title: "Annuler", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Confirmer", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
private func newUsername(textField: UITextField) {
usernameTextField = textField
usernameTextField?.borderStyle = .roundedRect
usernameTextField?.keyboardAppearance = .dark
usernameTextField?.placeholder = "Nouveau pseudo"
}
然后我打电话给presentUsernameAlert()
使用 AlertViewController
的 属性 open var textFields: [UITextField]? { get }
,经过可视化调试后我看到我们需要删除 0 中的 superView.superView.subView
,并更改 TextField.superView
背景清除修复问题。
试试这个代码:
private func presentUsernameAlert() {
let alert = UIAlertController(title: nil, message: "Alors ?", preferredStyle: .alert)
alert.addTextField(configurationHandler: newUsername)
alert.addAction(UIAlertAction(title: "Annuler", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Confirmer", style: .default, handler: nil))
present(alert, animated: true) {
}
if let textFields = alert.textFields {
if textFields.count > 0{
textFields[0].superview!.superview!.subviews[0].removeFromSuperview()
textFields[0].superview!.backgroundColor = UIColor.clear
}
}
}
最终应该是什么样子:
我在警报中有一个文本字段。 我已经像这样配置了它的 borderStyle:
textField.borderStyle = .roundedRect
但是如您所见,第一个边框周围还有另一个矩形边框:
我想通过代码将其删除,但找不到任何选项或操作方法。
这是警报代码:
private func presentUsernameAlert() {
let alert = UIAlertController(title: nil, message: "Alors ?", preferredStyle: .alert)
alert.addTextField(configurationHandler: newUsername)
alert.addAction(UIAlertAction(title: "Annuler", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Confirmer", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
private func newUsername(textField: UITextField) {
usernameTextField = textField
usernameTextField?.borderStyle = .roundedRect
usernameTextField?.keyboardAppearance = .dark
usernameTextField?.placeholder = "Nouveau pseudo"
}
然后我打电话给presentUsernameAlert()
使用 AlertViewController
的 属性 open var textFields: [UITextField]? { get }
,经过可视化调试后我看到我们需要删除 0 中的 superView.superView.subView
,并更改 TextField.superView
背景清除修复问题。
试试这个代码:
private func presentUsernameAlert() {
let alert = UIAlertController(title: nil, message: "Alors ?", preferredStyle: .alert)
alert.addTextField(configurationHandler: newUsername)
alert.addAction(UIAlertAction(title: "Annuler", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Confirmer", style: .default, handler: nil))
present(alert, animated: true) {
}
if let textFields = alert.textFields {
if textFields.count > 0{
textFields[0].superview!.superview!.subviews[0].removeFromSuperview()
textFields[0].superview!.backgroundColor = UIColor.clear
}
}
}
最终应该是什么样子: