美文网首页
iOS 键盘遮挡输入框的问题

iOS 键盘遮挡输入框的问题

作者: iOS门三闫 | 来源:发表于2019-11-05 10:44 被阅读0次

1.声明属性(记录将要编辑的输入框)

@property(nonatomic ,strong) UITextField * firstResponderTextF;(这里是记录将要编辑的输入框)

2.//监听键盘展示和隐藏的通知

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    //监听键盘展示和隐藏的通知

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

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}

3.#pragma maek UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditingNew:(UITextField *)textField{

    self.firstResponderTextF = textField;//当将要开始编辑的时候,获取当前的textField

    return YES;

}

- (BOOL)textFieldShouldReturnNew:(UITextField *)textField{

    [textFieldresignFirstResponder];

    return YES;

}

4.#pragma mark : UIKeyboardWillShowNotification/UIKeyboardWillHideNotification

- (void)keyboardWillShow:(NSNotification*)notification{

    CGRect rect = [self.firstResponderTextF.superview convertRect:self.firstResponderTextF.frame toView:self.view];//获取相对于self.view的位置

    NSDictionary*userInfo = [notificationuserInfo];

    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];//获取弹出键盘的fame的value值

    CGRectkeyboardRect = [aValueCGRectValue];

    keyboardRect = [self.view convertRect:keyboardRect fromView:self.view.window];//获取键盘相对于self.view的frame ,传window和传nil是一样的

    CGFloatkeyboardTop = keyboardRect.origin.y;

    NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//获取键盘弹出动画时间值

    NSTimeIntervalanimationDuration = [animationDurationValuedoubleValue];

    if(keyboardTop

        CGFloatgap = keyboardTop -CGRectGetMaxY(rect) -10;//计算需要网上移动的偏移量(输入框底部离键盘顶部为10的间距)

        __weaktypeof(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 = [notificationuserInfo];

    NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//获取键盘隐藏动画时间值

    NSTimeIntervalanimationDuration = [animationDurationValuedoubleValue];

    if (self.view.frame.origin.y < 0) {//如果有偏移,当影藏键盘的时候就复原

        __weaktypeof(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);

        }];

    }

}

5.// 退出键盘

-(void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent*)event{

    if ([self.firstResponderTextF isFirstResponder])[self.firstResponderTextF resignFirstResponder];

    [self.view endEditing:YES];

}

6.//移除键盘通知监听者

- (void)dealloc{

    self.countDowntor.delegate = nil;

    //移除键盘通知监听者

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];

}

相关文章

网友评论

      本文标题:iOS 键盘遮挡输入框的问题

      本文链接:https://www.haomeiwen.com/subject/xvrsbctx.html