Search code examples
iosswiftuiviewuitoolbarswift-extensions

iOS: UIToolbar or UIView for keyboard in swift


I am creating a UIView or a UIToolbar for my keyboard when I select a textfield. For example something like that in my viewDidLoad:

let customView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, 40))
        customView.backgroundColor = UIColor(colorLiteralRed: 0.0, green: 0.0, blue: 0.0, alpha: 0.2)
        emailTextField.inputAccessoryView = customView

and ok it works fine. But to optimise this cose, which is the best solution? For example, put this code in an extension? Or in a separated Xib? To have a clean code and not fill my viewController with these stuff.

Any ideas?

Thanks


Solution

  • if you want all the UITextField have this :

    let v = CustomView()
    let textField = UITextField.appearance()
    textField.inputAccessoryView = v
    

    or make an extension then call it when you want :

    extension UITextField{
        func addInputAccessoryView(){
            let v = CustomView()
            self.inputAccessoryView = v
        }
    }