iOS keyboard adaptive comment below the cell

First judge the click on the comment button event and reply to the comment event (also register to monitor the keyboard appearance and hide events)

Then get the coordinates of the clicked cell

Then calculate the height that needs to be adjusted when the keyboard comes out


  // NSNotification appears on the registration keyboard

    [[NSNotificationCenterdefaultCenter] addObserver:self

                                             selector:@selector(keyboardWillShow:)

                                                 name:UIKeyboardWillShowNotificationobject:nil];

    // Register the keyboard to hide NSNotification

    [[NSNotificationCenterdefaultCenter] addObserver:self

                                             selector:@selector(keyboardWillHide:)

                                                 name:UIKeyboardWillHideNotificationobject:nil];


   


//cell coordinates


@property (nonatomic,assign)CGRect rectSelected;



            NSIndexPath * indexPath2 = [ NSIndexPathindexPathForItem:index inSection:0];

            CGRect rectInTableView = [self.tableViewrectForRowAtIndexPath : indexPath2];

            self.rectSelected = [self.tableViewconvertRect:rectInTableViewtoView:[self.tableViewsuperview ]];//Converted to coordinates on self.view





#pragma mark

#pragma mark keyboardWillShow

- (void)keyboardWillShow:(NSNotification *)notification

{

    

    if (_KeySwitch) {

        

        return;

    }else{

    

        _KeySwitch =YES;

    }

    

    //[0](null) @"UIKeyboardFrameBeginUserInfoKey" : NSRect: { {0, 736}, {414, 293.66666666666674}}

    //    [1](null) @"UIKeyboardCenterEndUserInfoKey" : NSPoint: {207, 589.16666666666663}

    //    [2](null) @"UIKeyboardBoundsUserInfoKey" : NSRect: { {0, 0}, {414, 293.66666666666674}}

    //    [3](null) @"UIKeyboardFrameEndUserInfoKey" : NSRect: { {0, 442.33333333333326}, {414, 293.66666666666674}}

    //    [5](null) @"UIKeyboardCenterBeginUserInfoKey" : NSPoint: {207, 882.83333333333337}

    //    [4](null) @"UIKeyboardAnimationDurationUserInfoKey" : (double)0.25

    //    [6](null) @"UIKeyboardAnimationCurveUserInfoKey" : (long)7

    //    [7](null) @"UIKeyboardIsLocalUserInfoKey" : (int)1

    NSDictionary *userInfo = [notificationuserInfo];

    NSValue* aValue = [userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey];//aValueNSConcreteValue * NSRect: { {0, 442.33333333333326}, {414, 293.66666666666674}}0x0000000136c0c4b0

    __block CGFloat keyboardHeight = [aValueCGRectValue].size.height;//keyboardHeightCGFloat 293.66666666666674

    CGRect keyboardRect = [aValueCGRectValue];//keyboardRectCGRect (origin = (x = 0, y = 0), size = (width = 8, height = 0))    keyboardRect CGRect(origin = (x = 0, y = 442.33333333333326), size = (width = 414, height = 293.66666666666674))

    keyboardRect = [self.viewconvertRect:keyboardRectfromView:nil];rectview中转换到当前视图中,返回在当前视图中的rect

    

    CGFloat keyboardTop = keyboardRect.origin.y;//keyboardTopCGFloat 442.33333333333326

    CGRect newTextViewFrame =self.view.bounds;//newTextViewFrameCGRect (origin = (x = 0, y = 0), size = (width = 414, height = 442.33333333333326))     键盘上面文字区域的坐标

    newTextViewFrame.size.height = keyboardTop -self.view.bounds.origin.y;//上面文字的坐标 高度

    

    NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];//animationDurationNSTimeInterval 4.9406564584124654E-324

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    //history_Y_offset   btn转换成在self.view上的坐标

    CGFloat delta =self.rectSelected.origin.y + self.rectSelected.size.height - ([UIApplication sharedApplication].keyWindow.bounds.size.height - keyboardHeight-100);//deltaCGFloat -54.333333333333258

    

    --------------  计算tableview要偏移的距离   ----------

 


    CGPoint offset =self.tableView.contentOffset;//offsetCGPoint (x = 3.4540270524485753E-314, y = 2.1232730692355269E-314)   表视图的偏移量

    offset.y += delta;//offsetCGPoint (x = 0, y = -118.33333333333326)

    if (offset.y <0) {

        offset.y = -64;//offsetCGPoint (x = 0, y = -64)

    }

    [self.tableViewsetContentOffset:offset animated:YES];

}



#pragma mark

#pragma mark keyboardWillHide

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

    

    

    NSValue *animationDurationValue = [notification.userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    [UIViewanimateWithDuration:animationDurationanimations:^{

        //        [self.chatKeyBoard keyboardDownForComment];

        //        self.chatKeyBoard.placeHolder = nil;

    }];

}




Guess you like

Origin blog.csdn.net/qq_27247497/article/details/51776948