UITextfield 键盘外观的扩展

extension for UITextfield keyboard appearance

在我的应用程序中,我将添加不同的主题,但现在,我想编写一个扩展程序来处理扩展程序的键盘外观。 第一步,我只想控制它的外观,所以我设置它应该是 .dark 但它不起作用。你能告诉我这个简单扩展的问题在哪里吗? 我只是想让它自动改变键盘外观而不做任何其他事情

extension UITextView {


  var keyboardApperance: UIKeyboardAppearance? {
    get {
        return self.keyboardAppearance
    }
    set {
         self.keyboardAppearance = .dark
    }
  }
}

尝试使用这样的函数

extension UITextView {
    func setKeyboardToDark() {
        self.keyboardAppearance = .dark
    }

    func setKeyboardToLight() {
        self.keyboardAppearance = .light
    }
}

只创建一个扩展不会改变您的 UITextView 属性。

您可以创建自己的自定义 UITextView 并使用它代替 UITextView。

如果您使用故事板或 xib,请不要忘记设置 class。

@IBDesignable
public class CustomDarkTextView: UITextView {

    public override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setup()
    }

    public convenience init(frame: CGRect) {
        self.init(frame: frame, textContainer: nil)
        setup()
    }

    public convenience init() {
        self.init(frame: CGRect.zero, textContainer: nil)
        setup()
    }

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

    func setup() {
        self.keyboardAppearance = UIKeyboardAppearance.Dark
    }
}
  extension UITextView {
            func setDarkKeyboard() {
            self.keyboardAppearance = UIKeyboardAppearance.dark
           }

            func setLightKeyboard() {
            self.keyboardAppearance = UIKeyboardAppearance.light
          }
     }