3

I have a textField in an alert. I've configured its borderStyle like that:

textField.borderStyle = .roundedRect

But there's still another rect border around the first one as you can see:

I'd like to remove it by code but I don't find any options or what to do it.

Here is the alert code:

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"
}

then I call presentUsernameAlert()

lfalkau
  • 896
  • 7
  • 21
  • check if you have `textfield.layer.borderColor = UIColor.red.cgColor` in some part of your code – Reinier Melian Nov 30 '18 at 14:50
  • no, I don't have anything like that... – lfalkau Nov 30 '18 at 14:51
  • 1
    can you put all the AlertView relevant code in your question? – Reinier Melian Nov 30 '18 at 14:54
  • Yes no problem, let me two sec – lfalkau Nov 30 '18 at 14:54
  • Apple's controls are usually not very flexible, your options are either write your own version, modify it using private APIs or by going through the subviews (both which can get your app rejected or make it break eventually) or just using it as it is. I suggest you leave it as it is. – EmilioPelaez Nov 30 '18 at 14:57
  • I hope you're wrong because it's not really fancy – lfalkau Nov 30 '18 at 15:00
  • try using this property `open var textFields: [UITextField]? { get }` of `AlertViewController`, in your case should be index 0 and casting to `UITextField` set his borderWidth = 0 – Reinier Melian Nov 30 '18 at 15:02
  • Have a look at this: https://stackoverflow.com/questions/30040963/uitextfield-in-uialertcontroller-border-backgroundcolor . Borrowing the idea from here, I think you should write `for textField: UIView in alert.textFields! { let container: UIView = textField.superview! let effectView: UIView = container.superview!.subviews[0] container.backgroundColor = UIColor.clear effectView.removeFromSuperview() }` But call it after you present the alert. – surToTheW Nov 30 '18 at 15:23
  • I tried but I don't understand all of this – lfalkau Nov 30 '18 at 15:34

1 Answers1

2

Using this property open var textFields: [UITextField]? { get } of AlertViewController, after visual debugging I saw that we need to remove the superView.superView.subView in 0, and change the TextField.superView background to clear fix the issue.

Try this code:

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
        }
    }
}

What it should end up looking like:

Alert Textfield with Rounded Borders

Cal
  • 422
  • 6
  • 20
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55