简体   繁体   English

如何更改iOS 7中键盘的背景颜色

[英]How to change the background color of keypad in iOS 7

I am using Xcode 5 for developing applications. 我正在使用Xcode 5来开发应用程序。 I want to change the color of keyboard in iOS 7 . 我想在iOS 7中更改键盘的颜色。 I want a color like black or blue. 我想要一种像黑色或蓝色的颜色。

How can I change the color of keyboard in iOS 7? 如何在iOS 7中更改键盘的颜色?

You can only set the keyboard appearance to any of three listed below using the UIKeyboardAppearance . 您只能使用UIKeyboardAppearance将键盘外观设置为下面列出的三种中的任何UIKeyboardAppearance

UIKeyboardAppearanceDefault  // Corresponds to the UIKeyboardAppearanceLight
UIKeyboardAppearanceDark     // Available in iOS 7.0 and later.
UIKeyboardAppearanceLight    // Available in iOS 7.0 and later.

There is one more constant named UIKeyboardAppearanceAlert but, that is now deprecated. 还有一个名为UIKeyboardAppearanceAlert常量,但现在已弃用。 You should use UIKeyboardAppearanceDark instead. 您应该使用UIKeyboardAppearanceDark

You can't use any custom or undefined color. 不能使用任何自定义或未定义的颜色。 So, Use ... 所以,使用......

myTextfield.keyboardAppearance = UIKeyboardAppearanceDark;

In iOS 7, since the keyboard is translucent, I was able to accomplish this effect by adding a colored subview behind the keyboard that shows and hides with keyboard notifications . 在iOS 7中,由于键盘是半透明的,我可以通过在键盘后面添加彩色子视图来实现此效果,该子视图显示和隐藏键盘通知

I created a GitHub project to demonstrate this technique. 我创建了一个GitHub项目来演示这种技术。 Keep in mind it only works in portrait orientation right now and obviously only in iOS 7. 请记住,它现在仅适用于纵向,显然仅适用于iOS 7。

https://github.com/meekapps/TintedKeyboard https://github.com/meekapps/TintedKeyboard

在此输入图像描述

You can change the color with the keyboardAppearance method. 您可以使用keyboardAppearance方法更改颜色。

_textField.keyboardAppearance = UIKeyboardAppearanceDark;

Have a look at the Apple API docs 看看Apple API文档

Per your comment of wanting a custom color: 根据你想要自定义颜色的评论:

You could do this... Just use the normal keyboard, then observe UIKeyboardWillShowNotification and UIKeyboardWillHideNotification so that you can show a color UIView behind the keyboard. 可以这样做......只需使用普通键盘,然后观察UIKeyboardWillShowNotificationUIKeyboardWillHideNotification这样你就可以在键盘后面显示一个颜色UIView

It would be hacky, but it would work because the default keyboard is transparent to a degree on iOS 7. 这将是hacky,但它会工作,因为默认键盘在iOS 7上是透明的。

Good luck. 祝好运。

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

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