美文网首页
键盘遮挡textfield方案

键盘遮挡textfield方案

作者: Xiao_Li | 来源:发表于2017-09-26 14:48 被阅读18次
    - (void)keyboardWasShown:(NSNotification *)noti {
        NSDictionary *info = noti.userInfo;
        UITextField *activeField = noti.object;
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, kbSize.height, 0);
        self.originInset = self.scrollView.contentInset;
        self.scrollView.contentInset = contentInsets;
        self.scrollView.scrollIndicatorInsets = contentInsets;
    
        CGRect aRect = self.frame;
        aRect.size.height -= kbSize.height;
        if (!CGRectContainsPoint(aRect, activeField.frame.origin)) {
            CGPoint scrollPoint = CGPointMake(0, activeField.frame.origin.y - kbSize.height);
            [self.scrollView setContentOffset:scrollPoint animated:YES];
        }
    }
    
    - (void)keyboardWillBeHidden:(NSNotification *)noti {
        UIEdgeInsets contentInsets = self.originInset;
        self.scrollView.contentInset = contentInsets;
        self.scrollView.scrollIndicatorInsets = contentInsets;
    }
    

    相关文章

      网友评论

          本文标题:键盘遮挡textfield方案

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