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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9728847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:20:32  来源:igfitidea点击:

How to change keyboard background color in iOS?

iphoneiosiphone-keypad

提问by Matthieu Lucas

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

我想知道如何在 iOS 中以编程方式更改键盘背景颜色?背景通常是灰色的,但我已经看到黑色背景(在字母后面)。

回答by A-Live

For the dark background use:

对于深色背景使用:

mytextfield.keyboardAppearance = UIKeyboardAppearanceAlert;

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

阅读以了解有关UITextInputTraits 的更多信息(UIKeyboardAppearanceDark在 iOS 7+ 上使用)。

回答by Soheil Jadidian

To change it globally, you can use appearance proxy in AppDelegate... I've tested it in iOS 8, Swift:

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

UITextField.appearance().keyboardAppearance = UIKeyboardAppearance.dark

回答by Mr. T

In iOS 7, UIKeyboardAppearanceAlertis 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);

回答by Emmanouil Nicolas

Updating to swift 3.0

更新到 swift 3.0

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

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

回答by nambatee

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

如果您使用的是 Interface Builder,则可以在 Attributes Inspector 中设置键盘外观:

enter image description here

在此处输入图片说明

回答by Dani G.

SWIFT 4+: in the AppDelegate

SWIFT 4+:在 AppDelegate 中

UITextField.appearance().keyboardAppearance = .dark

回答by Mike

Today just use myTextField.keyboardAppearance = .dark

今天只用 myTextField.keyboardAppearance = .dark

回答by Mussa Charles

Swift 5+ (TextView)

Swift 5+ (TextView)

 textView.keyboardAppearance = .dark