textField,textview键盘遮挡

UIKeyboardType 11种键盘图片详解

https://www.jianshu.com/p/5d2a2c7aaf78

切换键盘

三方:IQKeyboardManager

https://www.jianshu.com/p/8412cfe5330f
1.自定义:frame
当我们有很多输入框时,有时候键盘弹出来会遮挡着输入框。我们需要获取输入框和键盘相对于最外层视图的位置来判断是否遮挡,如果遮挡了计算出遮挡的高度,然后设置最外层视图的frame,往上移动到大于等于遮挡遮住的高度即可。当键盘隐藏是在讲最外层视图的frame还原回来。

#import "ViewController.h"
@interface ViewController ()
@property(nonatomic ,strong) UITextField * firstResponderTextF;//记录将要编辑的输入框
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //监听键盘展示和隐藏的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)dealloc{
    //移除键盘通知监听者
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if ([self.firstResponderTextF isFirstResponder])[self.firstResponderTextF resignFirstResponder];
}
#pragma maek UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    self.firstResponderTextF = textField;//当将要开始编辑的时候,获取当前的textField
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark : UIKeyboardWillShowNotification/UIKeyboardWillHideNotification
- (void)keyboardWillShow:(NSNotification *)notification{
    CGRect rect = [self.firstResponderTextF.superview convertRect:self.firstResponderTextF.frame toView:self.view];//获取相对于self.view的位置
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];//获取弹出键盘的fame的value值
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:self.view.window];//获取键盘相对于self.view的frame ,传window和传nil是一样的
    CGFloat keyboardTop = keyboardRect.origin.y;
    NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//获取键盘弹出动画时间值
    NSTimeInterval animationDuration = [animationDurationValue doubleValue];
    if (keyboardTop < CGRectGetMaxY(rect)) {//如果键盘盖住了输入框
        CGFloat gap = keyboardTop - CGRectGetMaxY(rect) - 10;//计算需要网上移动的偏移量(输入框底部离键盘顶部为10的间距)
        __weak typeof(self)weakSelf = self;
        [UIView animateWithDuration:animationDuration animations:^{
            weakSelf.view.frame = CGRectMake(weakSelf.view.frame.origin.x, gap, weakSelf.view.frame.size.width, weakSelf.view.frame.size.height);
        }];
    }
}
- (void)keyboardWillHide:(NSNotification *)notification{
    NSDictionary *userInfo = [notification userInfo];
    NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//获取键盘隐藏动画时间值
    NSTimeInterval animationDuration = [animationDurationValue doubleValue];
    if (self.view.frame.origin.y < 0) {//如果有偏移,当影藏键盘的时候就复原
        __weak typeof(self)weakSelf = self;
        [UIView animateWithDuration:animationDuration animations:^{
            weakSelf.view.frame = CGRectMake(weakSelf.view.frame.origin.x, 0, weakSelf.view.frame.size.width, weakSelf.view.frame.size.height);
        }];
    }
}
@end

2.自定义:NSLayoutConstraint
现在项目都是stroyboard 的NSLayout了;
2.1 textfied 所在的页面的view,实现layout;
2.2 实现 键盘弹出的监听方法,
2.3 找到最顶端测view,修改它的top值;


屏幕快照 2019-05-23 下午5.30.24.png
屏幕快照 2019-05-23 下午5.39.06.png
屏幕快照 2019-05-23 下午5.36.07.png

然后:在对应的 键盘监听添加代码

- (void)keyboardWillShow:(NSNotification *)notification{//键盘出来
    [UIView animateWithDuration:0.5 animations:^{
        //执行的动画
        _topAoutLa.constant = -300;
        [self.view layoutIfNeeded];
        
    }];
}
- (void)keyboardWillHide:(NSNotification *)notification{//键盘消失
    [UIView animateWithDuration:0.5 animations:^{
        //执行的动画
        _topAoutLa.constant = 0;
        [self.view layoutIfNeeded];
        
    }];
}

键盘弹出类型的图

https://www.jianshu.com/p/5d2a2c7aaf78

你可能感兴趣的:(textField,textview键盘遮挡)