【问题标题】:Is there a way to disable transparency on the keyboard in iOS 7?有没有办法在 iOS 7 中禁用键盘上的透明度?
【发布时间】:2013-10-02 20:32:53
【问题描述】:

我想要一个带有非透明键盘的键盘 - 我无法使用任何受支持的 UIKeyboardTypes 来实现。有没有其他方法可以解决这个问题?

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

【问题讨论】:

    标签: ios keyboard ios7


    【解决方案1】:

    当应用程序在 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];
                         }];
    }
    

    【讨论】:

    • 如果 Apple 决定在 iOS 7.1 或 iOS 8 中进行任何更改,这将是一个很大的开销和相当的脾气。不过很聪明。
    • @KyleClegg :是的,但我没有看到任何其他方式。另外...他们是否更改键盘的任何内容都没关系,因为嗯...这是UIView
    • 是的。很好,您也在使用 kbFrame 而不仅仅是对帧进行硬编码 - 如果 iPhone 6 的外形尺寸再次发生变化,这可能会很有用。
    • 它不会让你的代码变坏,但是昨天没有使用基于块的动画是这样的(从 iOS4 开始),用块设置通知中心也是更好的做法。一点现代化一点也不会受到伤害,因为 iOS4 到现在正好是 4 年前发布的。
    • @holex :是的,昨天就是这样,特别是因为该解决方案是为 iOS7 设计的。我会更新答案。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      -(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,它给出了绝对颜色。
      请注意,仍然可以在击键时注意到模糊。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-03-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-12
          相关资源
          最近更新 更多