有没有办法告诉iOS更新视图控制器中的键盘外观?

时间:2019-05-03 10:51:55

标签: ios swift uiviewcontroller uikeyboard

我想在UIKeyboardAppearance中更新ViewController。我的意思是说,VC加载了UIKeyboardAppearance.default。如果我按下按钮,我希望键盘更新为.dark,现在让键盘在与.dark相同的VC中显示。

据我所知,iOS在加载VC时会检查UIKeyboardAppearance的值,并且直到再次加载VC才进行检查。即使您更改UIKeyboardAppearance的值并隐藏/显示键盘。

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

// creating a simple text box, and making the placeholder text the value of the keyboardAppearance
        myTextBox.backgroundColor = UIColor.lightGray
        myTextBox.frame = CGRect(x: 30, y: 200, width: 300, height: 50)
        view.addSubview(myTextBox)
        UITextField.appearance().keyboardAppearance = .dark
        myTextBox.becomeFirstResponder()
        myTextBox.placeholder = "Keybaoard Appearance is: \(UITextField.appearance().keyboardAppearance.rawValue)"

// a simple button to toggle the keyboardAppearance
        toggleButton.frame = CGRect(x: 30, y: 300, width: 300, height: 50)
        toggleButton.setTitle("Toggle Keyboard", for: .normal)
        toggleButton.backgroundColor = UIColor.red
        toggleButton.addTarget(self, action: #selector(toggleButtonFunction), for: .touchUpInside)
        view.addSubview(toggleButton)

    }

// toggles the keyboardAppearance. Hides the keyboard, and a second later shows it again.
    @objc func toggleButtonFunction() {
        if UITextField.appearance().keyboardAppearance == .dark {
            UITextField.appearance().keyboardAppearance = .default
        }
        else {
            UITextField.appearance().keyboardAppearance = .dark
        }
        myTextBox.resignFirstResponder()
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
            self.myTextBox.becomeFirstResponder()
            self.myTextBox.placeholder = "Keybaoard Appearance is: \(UITextField.appearance().keyboardAppearance.rawValue)"

            })
    }

    let myTextBox = UITextField()
    let toggleButton = UIButton()
}

我希望在更改UIKeyboardAppearance并隐藏/显示键盘后,它将以新的外观(.dark.default)显示,但是它会以相同的外观连续显示直到再次加载VC。您可以看到UIKeyboardAppearance的更改值,但是iOS似乎直到VC重新加载后才检查该更新。

有什么方法可以强制在VC中进行重新检查吗?

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您可以在屏幕上递归更改所有文本字段的键盘外观(allSubviewsOf(type:)扩展名来自this great answer by Mohammad Sadiq

func changeTextFieldKeyboardAppearance() {
    UITextField.appearance().keyboardAppearance = .dark

    let textFields = view.allSubviewsOf(type: UITextField.self)

    let firstResponder = textFields.first { $0.isFirstResponder }

    firstResponder?.resignFirstResponder()

    textFields.forEach { $0.keyboardAppearance = .dark }

    firstResponder?.becomeFirstResponder()
}

[...]

extension UIView {
    func allSubviewsOf<T: UIView>(type: T.Type) -> [T] {
        var all = [T]()

        func getSubview(view: UIView) {
            if let aView = view as? T {
                all.append(aView)
            }

            guard !view.subviews.isEmpty else {
                return
            }

            view.subviews.forEach{ getSubview(view: $0) }
        }

        getSubview(view: self)

        return all
    }
}

如果您的视图控制器嵌入在UITabBarController中,则可以通过更改其selectedIndex并将其立即更改回原始索引来触发更新:

guard let tabBarController = tabBarController else {
    return
}

let selectedIndex = tabBarController.selectedIndex

UITextField.appearance().keyboardAppearance = .dark

tabBarController.selectedIndex = selectedIndex == 1 ? 0 : 1
tabBarController.selectedIndex = selectedIndex

答案 1 :(得分:0)

感谢塔玛斯的回答!

它引导我走上发现自己需要的东西的道路。

好像您更改了UITextField的keyboardAppearance

UITextField.appearance().keyboardAppearance = .dark

系统仅检查VC负载。如果您为每个textField更改它

myTextBox.keyboardAppearance = .dark

系统将在每次首次响应者更改时检查并加载正确的键盘。

再次感谢塔玛斯!