您的位置:首页 > 其它

当键盘挡住输入框时候,可以使的界面自动上移

2016-06-13 17:43 543 查看
第一步:注册通知:

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

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

    

第二步:实现方法:

- (void)keyboardWillShow:(NSNotification *)notification

{

    NSDictionary * info = [notification userInfo];

    NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [self.view convertRect:[avalue CGRectValue] fromView:nil];

    double keyboardHeight=keyboardRect.size.height;//键盘的高度

   

    CGFloat offset = self.view.frame.size.height - ([addressTextFile superview].frame.origin.y + remarksTextView.frame.size.height + keyboardHeight + 50 * heightScale);

    if (offset <= 0) {

        [UIView animateWithDuration:0.3 animations:^{

            

            CGRect frame = self.view.frame;

            frame.origin.y = offset;

            self.view.frame = frame;

            

        }];

    }

}

- (void)keyboardHide:(UITapGestureRecognizer*)tap{

    [nameTextFile resignFirstResponder];

    [phoneTextFile resignFirstResponder];

    [addressTextFile resignFirstResponder];

    [remarksTextView resignFirstResponder];

    

}

//第三步:移除通知

-(void)viewDidDisappear:(BOOL)animated {

    

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

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

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  界面 键盘 keyBoard