[英]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:如果您需要同时支持早期的 iOS 和 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
SWIFT 4+:在 AppDelegate 中
UITextField.appearance().keyboardAppearance = .dark
今天只需使用myTextField.keyboardAppearance = .dark
Swift 5+ (TextView)
textView.keyboardAppearance = .dark
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.