49

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).

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Matthieu Lucas
  • 817
  • 1
  • 9
  • 15

9 Answers9

53

For the dark background use:

mytextfield.keyboardAppearance = UIKeyboardAppearanceAlert;

Read to find more information about UITextInputTraits (use UIKeyboardAppearanceDark at iOS 7+).

Cœur
  • 37,241
  • 25
  • 195
  • 267
A-Live
  • 8,904
  • 2
  • 39
  • 74
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
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);
Community
  • 1
  • 1
Mr. T
  • 12,795
  • 5
  • 39
  • 47
  • 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

5

If you're using Interface Builder, you can set keyboard look in the Attributes Inspector:

enter image description here

nambatee
  • 1,478
  • 16
  • 27
1

SWIFT 4+: in the AppDelegate

UITextField.appearance().keyboardAppearance = .dark
Dani G.
  • 577
  • 8
  • 21
1

Today just use myTextField.keyboardAppearance = .dark

Mike
  • 817
  • 1
  • 8
  • 15
0

Swift 5+ (TextView)

 textView.keyboardAppearance = .dark
Mussa Charles
  • 4,014
  • 2
  • 29
  • 24
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