简体   繁体   English

如何在 iOS 中更改键盘背景颜色?

[英]How to change keyboard background color in iOS?

I would like to know how to change the keyboard background color programmatically in iOS?我想知道如何在 iOS 中以编程方式更改键盘背景颜色? The background is normally grey but I have already seen black background (behind letters).背景通常是灰色的,但我已经看到黑色背景(字母后面)。

For the dark background use:对于深色背景使用:

mytextfield.keyboardAppearance = UIKeyboardAppearanceAlert;

Read to find more information about UITextInputTraits (use UIKeyboardAppearanceDark at iOS 7+).阅读以了解有关UITextInputTraits 的更多信息(在 iOS 7+ 上使用UIKeyboardAppearanceDark )。

要全局更改它,您可以在 AppDelegate 中使用外观代理...我已经在 iOS 8、Swift 中对其进行了测试:

UITextField.appearance().keyboardAppearance = UIKeyboardAppearance.dark

In iOS 7, UIKeyboardAppearanceAlert is deprecated, so use this instead:在 iOS 7 中,不推荐使用UIKeyboardAppearanceAlert ,因此请改用它:

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:如果您需要同时支持早期的 iO​​S 和 iOS 7,并且您已经创建了必要的宏(根据https://stackoverflow.com/a/5337804/588253 ),您可以使用这个:

mytextfield.keyboardAppearance = (SYSTEM_VERSION_LESS_THAN(@"7.0") ? UIKeyboardAppearanceAlert : UIKeyboardAppearanceDark);

更新到 swift 3.0

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

If you're using Interface Builder, you can set keyboard look in the Attributes Inspector:如果您使用的是 Interface Builder,则可以在 Attributes Inspector 中设置键盘外观:

在此处输入图片说明

SWIFT 4+:在 AppDelegate 中

UITextField.appearance().keyboardAppearance = .dark

今天只需使用myTextField.keyboardAppearance = .dark

Swift 5+ (TextView)

 textView.keyboardAppearance = .dark

In Objectve-C在 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.您可以在 AppDelegate 中定义它以全局处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM