Getting the background color of a UIKeyboard

Is there a way to get the background color of a UIKeyboard ? I put an auxiliary view on top of my UIKeyboard and try to match its color with the keyboard background color. But it seems that different types of keyboards have different background colors. Below are screenshots for the default keyboard and email.

Is there any way we can programmatically determine the background color of the keyboard so that the color of the accessoryView can be changed.

UIKeyboardTypeEmailAddress

UIKeyboardTypeDefault

+7
source share
5 answers

You can do something like this:

 UIKeyboardAppearance currentAppearance = yourTextView.keyboardAppearance; if (currentAppearance == UIKeyboardAppearanceDark) { // dark } else if (currentAppearance == UIKeyboardAppearanceDefault) { // default } else if (currentAppearance == UIKeyboardAppearanceLight) { // light } 
+3
source

I suggest you get the color of your RGB keyboard from DigitalColor Meter , since we use this tool on a Mac or you can get it with any other tool.

And then just assign this RGB value to your accessory and match its color to the keyboard.

Your keyboard's RGB value, it seems to me, is similar to (63,63,63) , and you can use this:

 [UIColor colorWithRed:63/255.0 green:63/255.0 blue:63/255.0 alpha:1] 

Hope this helps you.

+2
source

For a dark background use

  mytextfield.keyboardAppearance = UIKeyboardAppearanceAlert; 
0
source

Swift 5

You can use the input view and it matches the keyboard style.

 let text = UITextView() text.inputAccessoryView = UIInputView(frame: .init(x: 0, y: 0, width: 0, height: 40), inputViewStyle: .keyboard) 
0
source

If you are developing a custom keyboard and trying to match it with the background of the keyboard view, use this in your keyboard view controller, where you set up the view:

 keyboardView.backgroundColor = view.backgroundColor 

My viewDidLoad() in my keyboard view controller looks like this:

 var keyboardView: UIView! let keyboardNib = UINib(nibName: "KeyboardView", bundle: nil) keyboardView = keyboardNib.instantiate(withOwner: self, options: nil) [0] as? UIView keyboardView.frame.size = view.frame.size keyboardView.backgroundColor = view.backgroundColor view.addSubview(keyboardView) 
0
source

Source: https://habr.com/ru/post/969727/


All Articles