• iOS 关于第三方键盘


    第三方键盘和系统键盘的区别:

          监听方法:[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    系统键盘会调用一次,keyboardWillShow:,而第三方键盘会调用三次。

    出现的问题

          在一些页面,textField 在页面底部,弹出键盘时会遮挡输入框,需要计算上移的高度,做一些上移。

      我是这么来做的

    - (void)keyboardWillShow:(NSNotification *)notification
    {
        NSDictionary *userInfo = [notification userInfo];
        NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardRect = [aValue CGRectValue];
        
        NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
        NSTimeInterval animationDuration;
        [animationDurationValue getValue:&animationDuration];
        
        CGRect responderConvertedFrame = [_firstRespondTF.superview convertRect:_firstRespondTF.frame toView:self.view];
        CGRect keyboardConvertedFrame = [self.view.window convertRect:keyboardRect toView:self.view];
        CGFloat needOffsetY = responderConvertedFrame.origin.y + responderConvertedFrame.size.height - keyboardConvertedFrame.origin.y;
        
        // 如果要移动距离大于0说明有遮盖,说明需要移动
        if (needOffsetY > 0) {
            // scrollView现在的偏移量 + 需要移动的偏移量 = 最终要设置的偏移量
            CGFloat offsetY = needOffsetY + self.scrollView.contentOffset.y;
            CGFloat maxOffsetY = self.scrollView.contentSize.height - self.scrollView.height;
            if (offsetY <= maxOffsetY) {
                self.contentInsetBottom = 0;
                [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x, offsetY) animated:YES];
            }else {
                self.contentInsetBottom = ceil(offsetY - maxOffsetY);
                self.scrollView.contentInset = UIEdgeInsetsMake(self.scrollView.contentInset.top, self.scrollView.contentInset.left, self.scrollView.contentInset.bottom + self.contentInsetBottom, self.scrollView.contentInset.right);
                [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x, offsetY) animated:YES];
            }
        }
    }
    
    
    - (void)keyboardWillHide:(NSNotification *)notification
    {
        self.scrollView.contentInset = UIEdgeInsetsMake(self.scrollView.contentInset.top, self.scrollView.contentInset.left, self.scrollView.contentInset.bottom - self.contentInsetBottom, self.scrollView.contentInset.right);
        self.contentInsetBottom = 0;
    }

      系统键盘时,完全没有问题;

      当是第三方键盘时,由于走了三遍,就出现了错误。

    解决问题

             第一种方法:禁用第三方键盘

              很多文章都提到了下面这个方法:

    - (BOOL)application:(UIApplication *)application shouldAllowExtensionPointIdentifier:(NSString *)extensionPointIdentifier  {
         return NO;
    }

           该方法的确可以禁用掉第三方键盘,但是整个APP 中,都禁用掉了。而有时候,我们只是在某些界面禁用第三方键盘。

           第二种方法:利用 textField 的 secureTextEntry 属性

             当该属性被设置为 YES 时,可能是苹果为了安全考虑,第三方键盘是无法弹出的。当然你可能说,这样输入的内容就变成黑点了,我们可以在键盘出现之后再改回来。

             我们在该方法中,设置一下

    - (void)textFieldDidBeginEditing:(UITextField *)textField {
      if (textField == _agencyTF) {
        textField.secureTextEntry = YES;
        textField.font = nil;
        textField.font = FONTSIZE(14);
      }
    }

       然后再设置回来

    - (void)keyboardWillShow:(NSNotification *)notification{  
      if (_agencyTF.secureTextEntry == YES) {
        _agencyTF.secureTextEntry = NO;
        _agencyTF.font = nil;
        _agencyTF.font = FONTSIZE(14);
      }
    }

      第三种方法:推荐使用第三方 IQKeyboardManager

        使用IQKeyboardManager可以很容易地解决弹起键盘遮盖输入框的问题。

            地址:https://github.com/hackiftekhar/IQKeyboardManager

    你还有什么好的解决方法?欢迎留言讨论

  • 相关阅读:
    11111
    单例-Singleton-03
    单例-Singleton-02
    单例-Singleton-01
    load和initialize
    OC中的static-01
    GCD-06
    GCD-05
    GCD-03
    UIView-01
  • 原文地址:https://www.cnblogs.com/lion-witcher/p/10905636.html
Copyright © 2020-2023  润新知