I would like to know how to change the keyboard background color programmatically in iOS? The background is normally grey but I have already seen black background (behind letters).
Asked
Active
Viewed 5.3k times
49
-
sorry about the close vote; I misread the question - edited for clarification. – Thomas Shields Mar 15 '12 at 22:08
9 Answers
53
For the dark background use:
mytextfield.keyboardAppearance = UIKeyboardAppearanceAlert;
Read to find more information about UITextInputTraits (use UIKeyboardAppearanceDark
at iOS 7+).
38
To change it globally, you can use appearance proxy in AppDelegate... I've tested it in iOS 8, Swift:
UITextField.appearance().keyboardAppearance = UIKeyboardAppearance.dark

Tunscopi
- 97
- 2
- 8

Soheil Jadidian
- 878
- 7
- 12
-
1This can also be used for setting the colour of the keyboard for swiftui – BrettS Jan 08 '20 at 17:29
27
In iOS 7, UIKeyboardAppearanceAlert
is deprecated, so use this instead:
mytextfield.keyboardAppearance = UIKeyboardAppearanceDark;
If you need to support both earlier iOSes and iOS 7, and you've created the necessary macros (per https://stackoverflow.com/a/5337804/588253), you can use this:
mytextfield.keyboardAppearance = (SYSTEM_VERSION_LESS_THAN(@"7.0") ? UIKeyboardAppearanceAlert : UIKeyboardAppearanceDark);
-
Thanks for the answer above, I was wondering if you'd happen to know why this won't compile when I'm building for a target of iOS 6 and also supporting iOS7? – Oct 18 '13 at 15:19
10
Updating to swift 3.0
let textFieldAppearance = UITextField.appearance()
textFieldAppearance.keyboardAppearance = .dark //.default//.light//.alert

Emmanouil Nicolas
- 134
- 1
- 4
-
Is there a reason not to do just UITextField.appearance().keyboardAppearance = .dark ? – Leon Jun 14 '17 at 16:18
-
-
4
-
`UITextField.appearance().keyboardAppearance = .dark` in AppDelegate will change the appearance globally. with this solution you can do it on specific text fields. – Nitin Nain Jul 23 '18 at 05:57
1
SWIFT 4+: in the AppDelegate
UITextField.appearance().keyboardAppearance = .dark

Dani G.
- 577
- 8
- 21
-
-
@randomcontrol Did you try `textView.keyboardAppearance`? The property comes from `UITextInputTraits` – Harry Ng Sep 25 '19 at 10:16
0
In Objectve-C
For Dark Keyboard
[UITextField appearance].keyboardAppearance = UIKeyboardAppearanceDark;
For Light keyboard
[UITextField appearance].keyboardAppearance = UIKeyboardAppearanceLight;
You can define this in AppDelegate to handle this globally.

Rohit Singh
- 16,950
- 7
- 90
- 88