有没有办法在iOS 7中禁用键盘上的透明度?

时间:2013-09-26 18:10:59

标签: ios keyboard ios7

我想要一个带有非透明键盘的键盘 - 我无法使用任何支持的UIKeyboardTypes。还有另一种解决方法吗?

我想我可以用键盘上的背景视图覆盖我想要的颜色 - 是否有一种很好的方法可以与键盘节目动画同步动画该背景视图?

4 个答案:

答案 0 :(得分:4)

当使用iOS 7的基本SDK在Xcode 5中编译应用程序时,iOS7中的键盘是半透明的 如果您在Xcode 4.6.x上构建应用程序,您将拥有如前所述的非半透明键盘 (我知道这是一个糟糕的解决方案但是,我认为我会建议它)

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

  1. UIKeyboardWillShowNotification
  2. UIKeyboardWillHideNotification
  3. 应该是这样的:

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

答案 1 :(得分:0)

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

我能想到的另一种方法是设计自己的键盘,但这是一个繁琐的过程。

答案 2 :(得分:0)

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

为了工作,我为键盘控件(inputAccessoryViewUITextView)设置了UITextField。在我设置为UIView的{​​{1}}课程中,我添加了以下内容:

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]; } } 是我在init方法中设置的_lKeyboardBackground

CALayer

这理论上应该通过Apple的批准,但我从未测试过。还有很多变化,这在以后的版本或其他一些情况下都不会起作用(例如当iPad上有分离式键盘时)

我使用的

_lKeyboardBackground = [CALayer layer]; _lKeyboardBackground.backgroundColor = [[UIColor redColor] CGColor]; //or some less annoying color 是1,它给出了绝对颜色 请注意,仍然可以在击键时注意到模糊。

答案 3 :(得分:0)

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