10

我想要一个带有非透明键盘的键盘 - 我无法使用任何受支持的 UIKeyboardTypes 来获得它。还有其他方法吗?

我想我可以用我想要的颜色覆盖键盘下方的背景视图 - 有没有一种好方法可以让背景视图与键盘显示动画同步?

4

4 回答 4

4

当应用程序在 Xcode 5 中使用 iOS 7 的 Base SDK 编译时,iOS7 中的键盘是半透明的。
如果您在 Xcode 4.6.x 上构建应用程序,您将像以前一样拥有非半透明键盘。
(我知道这是一个糟糕的解决方案,但尽管如此,我想我会建议它)

无论如何,您也可以尝试使用默认键盘通知:

  1. UIKeyboardWillShowNotification
  2. UIKeyboardWillHideNotification

应该是这样的:

-(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];
                     }];
}
于 2013-10-30T09:12:40.100 回答
0

Apple 不允许任何人修改默认键盘。如果您打算使用 iOS 7,那么您将不得不处理半透明键盘。

我能想到的唯一其他方法是设计自己的键盘,但这是一个乏味的过程。

于 2013-10-30T09:16:12.397 回答
0

使用键盘通知在键盘后面显示/隐藏自定义黑色视图(如果使用白色键盘,则为白色),瞧,不再透明。

于 2014-07-17T21:09:20.577 回答
0

我今天也在研究同样的事情,我找到了一个简单的解决方法(虽然,还不确定它有多可靠)。

为了工作,我inputAccessoryView为我的键盘控件(UITextViewUITextField)设置了一个。在UIView我设置的类中,inputAccessoryView我添加了以下内容:

-(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];
    }
}

_lKeyboardBackgroundCALayer我在 init 方法中设置的:

_lKeyboardBackground = [CALayer layer];
_lKeyboardBackground.backgroundColor = [[UIColor redColor] CGColor]; //or some less annoying color

这理论上应该通过苹果的批准,但我从未测试过。还有很多更改在未来的版本中或在某些其他情况下将不起作用(例如,当 iPad 上有分体键盘时)

lMagicLayerIndex我使用的是 1,它给出了绝对颜色。
请注意,仍然可以在击键时注意到模糊。

于 2014-06-04T15:20:44.250 回答