UIAlertController - change size of text fields and add space between them

前端 未结 2 2082
抹茶落季
抹茶落季 2020-12-21 01:05

My alert looks like this:

Is it possible to make inputs bigger and add space between them? Here is a snippet from my code. I tried changing the frame<

相关标签:
2条回答
  • 2020-12-21 01:38

    WARNING: only use this solution as a last resort! If you want to fine-tune the look of your alert controller, use a custom alert view. Take a look at this question for details.

    As a hacky solution, you can use a third text field between the two as a separator and disable it. Also, you can give it a custom height using this mehtod.

    To avoid getting this extra text field in focus, use the UITextFieldDelegate method textFieldShouldReturn(_:) to get the second text field to focus instead.

    class AlertViewController: UITextFieldDelegate {
        [...]
    
        var firstTextField: UITextField!
        var secondTextField: UITextField!
    
        func setUpAlert() {
            [...]
    
            alert.addTextField { [weak self] textField in
                self?.firstTextField = textField
                textField.delegate = self
            }
    
            alert.addTextField { textField in
                textField.addConstraint(textField.heightAnchor.constraint(equalToConstant: 2))
                textField.isEnabled = false
            }
    
            alert.addTextField { [weak self] textField in
                self?.secondTextField = textField
            }
    
            [...]
        }
    
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            if textField == firstTextField {
                secondTextField?.becomeFirstResponder()
            }
    
            return true
        }
    }
    

    Result:

    Unfortunately, you cannot hide the extra text field, but at least you can give it a background:

    textField.backgroundColor = .black
    

    Result:

    0 讨论(0)
  • 2020-12-21 01:39

    UIAlertController views are intended to be simple and not customizable. If you make your own presented view controller, then the view belongs to you and you can do anything you like.

    0 讨论(0)
提交回复
热议问题