How to change keyboard background color in iOS?
Asked Answered
T

9

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

Trip answered 15/3, 2012 at 22:1 Comment(1)
sorry about the close vote; I misread the question - edited for clarification.Kinson
W
53

For the dark background use:

mytextfield.keyboardAppearance = UIKeyboardAppearanceAlert;

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

Willette answered 15/3, 2012 at 22:36 Comment(0)
P
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
Pilarpilaster answered 29/7, 2015 at 22:37 Comment(1)
This can also be used for setting the colour of the keyboard for swiftuiGuiltless
U
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://mcmap.net/q/23741/-how-to-check-ios-version), you can use this:

mytextfield.keyboardAppearance = (SYSTEM_VERSION_LESS_THAN(@"7.0") ? UIKeyboardAppearanceAlert : UIKeyboardAppearanceDark);
Ulla answered 24/9, 2013 at 21:22 Comment(1)
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?Insessorial
V
10

Updating to swift 3.0

let textFieldAppearance = UITextField.appearance() textFieldAppearance.keyboardAppearance = .dark //.default//.light//.alert

Versicle answered 13/12, 2016 at 16:3 Comment(4)
Is there a reason not to do just UITextField.appearance().keyboardAppearance = .dark ?Bugg
No, you can do this way too.Versicle
I know, I was asking if there's a reason you don't do it the short way?Bugg
UITextField.appearance().keyboardAppearance = .dark in AppDelegate will change the appearance globally. with this solution you can do it on specific text fields.Rois
D
5

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

enter image description here

Delinquent answered 29/1, 2019 at 16:33 Comment(0)
M
2

Today just use myTextField.keyboardAppearance = .dark

Misvalue answered 11/12, 2019 at 13:18 Comment(1)
Good to know and happy to help!Misvalue
K
1

SWIFT 4+: in the AppDelegate

UITextField.appearance().keyboardAppearance = .dark
Katz answered 21/4, 2019 at 10:56 Comment(2)
I need the same thing for UITextView but it doesn't exist.Congeal
@Congeal Did you try textView.keyboardAppearance? The property comes from UITextInputTraitsSumptuary
A
0

Swift 5+ (TextView)

 textView.keyboardAppearance = .dark
Autogiro answered 25/3, 2020 at 18:9 Comment(0)
W
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.

Wizen answered 14/4, 2022 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.