iOS: InputAccessoryView UITextField не обновляется после поворота - PullRequest
0 голосов
/ 27 февраля 2019

Я написал пользовательский UITextField и добавил кнопку над клавиатурой, используя inputAccessoryView.

. Вот код:

class CustomTextField: UITextField {

    var button: UIButton?

    var buttonContainer: UIView?

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        redraw()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        redraw()
    }

    private func redraw() {
        button = UIButton()
        button?.setTitle("BUTTON TEST", for: .normal)
        button?.backgroundColor = .red
        let dividend: CGFloat = isPortraitOrientation ? 56 : 1
        let divisor: CGFloat = isPortraitOrientation ? 326 : 18
        let buttonHeight = ((UIScreen.main.bounds.width - 48) * dividend) / divisor
        button?.frame = CGRect(origin: CGPoint(x: 24, y: 0), size: CGSize(width: UIScreen.main.bounds.width - 48, height: buttonHeight))
        buttonContainer = UIView()
        buttonContainer?.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: buttonHeight + 10)
        buttonContainer?.backgroundColor = .clear
        inputAccessoryView = buttonContainer!
        buttonContainer?.addSubview(button!)
    }

    func updateView() {
        redraw()
    }

}

И в контроллере:

class CustomViewController: UIViewController {

    @IBOutlet weak var textField: CustomTextField!

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        coordinator.animate(alongsideTransition: nil) { _ in
            self.textField.updateView()
        }
    }
}

И button, и buttonContainer отображаются правильно, когда я сначала поворачиваю экран, затем начинаю редактировать текстовое поле.Но они не правильно перерисовываются при повороте телефона.

Спасибо за помощь.

...