【发布时间】:2013-10-02 20:32:53
【问题描述】:
我想要一个带有非透明键盘的键盘 - 我无法使用任何受支持的 UIKeyboardTypes 来实现。有没有其他方法可以解决这个问题?
我想我可以用我想要的颜色覆盖键盘下方的背景视图 - 有没有一种好方法可以让背景视图与键盘显示动画同步?
【问题讨论】:
我想要一个带有非透明键盘的键盘 - 我无法使用任何受支持的 UIKeyboardTypes 来实现。有没有其他方法可以解决这个问题?
我想我可以用我想要的颜色覆盖键盘下方的背景视图 - 有没有一种好方法可以让背景视图与键盘显示动画同步?
【问题讨论】:
当应用程序在 Xcode 5 中使用 iOS 7 的 Base SDK 编译时,iOS7 中的键盘是半透明的。
如果您在 Xcode 4.6.x 上构建应用程序,您将像以前一样拥有非半透明键盘。
(我知道这是一个糟糕的修复,但我想我会建议它)
无论如何,您也可以尝试使用默认键盘通知:
UIKeyboardWillShowNotificationUIKeyboardWillHideNotification应该是这样的:
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)keyboardWillShow:(NSNotification *)note
{
/*
Would have used:
CGRect rectStart = [note.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect rectEnd = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
Reason for not using:
The above two keys are not being used although, ideally, they should have been
since they seem to be buggy when app is in landscape mode
Resolution:
Using the deprecated UIKeyboardBoundsUserInfoKey since it works more efficiently
*/
CGRect rectStart_PROPER = [note.userInfo[UIKeyboardBoundsUserInfoKey] CGRectValue];
rectStart_PROPER.origin.y = self.view.frame.size.height;
UIView *vwUnderlay = [self.view viewWithTag:8080];
if (vwUnderlay) {
[vwUnderlay removeFromSuperview];
}
vwUnderlay = [[UIView alloc] init];
[vwUnderlay setFrame:rectStart_PROPER];
[vwUnderlay setBackgroundColor:[UIColor orangeColor]];
[vwUnderlay setTag:8080];
[self.view addSubview:vwUnderlay];
[UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]
delay:0
options:[note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16
animations:^{
[vwUnderlay setFrame:CGRectOffset(vwUnderlay.frame, 0, -vwUnderlay.frame.size.height)];
}
completion:nil];
}
-(void)keyboardWillHide:(NSNotification *)note
{
UIView *vwUnderlay = [self.view viewWithTag:8080];
[UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]
delay:0
options:[note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16
animations:^{
[vwUnderlay setFrame:CGRectOffset(vwUnderlay.frame, 0, vwUnderlay.frame.size.height)];
}
completion:^(BOOL finished){
[vwUnderlay removeFromSuperview];
}];
}
【讨论】:
UIView
Apple 不允许任何人修改默认键盘。如果您打算使用 iOS 7,那么您将不得不处理半透明键盘。
我能想到的唯一其他方法是设计自己的键盘,但这是一个乏味的过程。
【讨论】:
我今天也在研究同样的事情,我找到了一个简单的解决方法(尽管还不确定它的可靠性)。
为了工作,我为我的键盘控件设置了一个inputAccessoryView(UITextView 和UITextField)。在我设置为inputAccessoryView 的UIView 类中,我添加了以下内容:
-(void)layoutSubviews{
[super layoutSubviews];
frame = _lKeyboardBackground.frame;
frame.origin.y = [self convertPoint:self.frame.origin toView:self.superview].y+self.frame.size.height;
frame.size.width = self.bounds.size.width;
frame.origin.x = 0;
frame.size.height = 500;
_lKeyboardBackground.frame = frame;
[self refreshKeyboardBackground];
}
-(void)didMoveToSuperview{
[super didMoveToSuperview];
if (self.superview) {
[self.superview.layer insertSublayer:_lKeyboardBackground atIndex:lMagicLayerIndex];
}
else {
[_lKeyboardBackground removeFromSuperlayer];
}
}
-(void)setFrame:(CGRect)frame{
[super setFrame:frame];
[self refreshKeyboardBackground]; // setFrame: is called when keyboard changes (e.g: to a custom input view)
}
-(void)refreshKeyboardBackground{
if (_lKeyboardBackground.superlayer) {
CALayer *parent = _lKeyboardBackground.superlayer;
[_lKeyboardBackground removeFromSuperlayer];
[parent insertSublayer:_lKeyboardBackground atIndex:lMagicLayerIndex];
}
}
_lKeyboardBackground 是我在 init 方法中设置的CALayer:
_lKeyboardBackground = [CALayer layer];
_lKeyboardBackground.backgroundColor = [[UIColor redColor] CGColor]; //or some less annoying color
理论上这应该通过Apple的批准,但我从未测试过。还有很多更改在未来的版本中或在某些其他情况下(例如 iPad 上有分体键盘时)将不起作用
我使用的lMagicLayerIndex 是 1,它给出了绝对颜色。
请注意,仍然可以在击键时注意到模糊。
【讨论】:
使用键盘通知在键盘后面显示/隐藏自定义黑色视图(如果您使用白色键盘,则为白色),瞧,不再透明。
【讨论】: