UITextView resize when keyboard is open calculatio

2019-09-16 10:57发布

I am trying to resize the UITextView when the keyboard is open.

in order to give my UITextView a new size ( so that it doesn't become shadowed by the keyboard) I make the following calculation

firstResult = UITextView bottom coordinate - keyboard top coordinate

firstResult should now have the size of the shadowed UITextView frame

then i do textView.frame.size.height -= firstResult which now should have a new size that would not be shadowed by the keyboard.

The problem with the code as it stands out is that there is always part of the UIView that is hidden behind the keyboard.

Could anyone point out what's wrong with my calculations so that the new size is always right? or any other way that I could use to resize the UITextView appropriately because all examples I find online do not work somehow.

the code

- (void)keyboardWasShown:(NSNotification *)notification {
CGRect viewFrame = input.frame;
    CGFloat textEndCord = CGRectGetMaxY(input.frame);
    CGFloat kbStartCord = input.frame.size.height - ([[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]).size.height;

    CGFloat result = fabsf( input.frame.size.height - fabsf( textEndCord - kbStartCord ));
    viewFrame.size.height -= result;
    NSLog(@"Original Height:%f, TextView End Cord: %f, KB Start Cord: %f, resutl: %f, the sum: %f",input.frame.size.height, textEndCord,kbStartCord,result,fabsf( textEndCord - kbStartCord ));
    input.frame = viewFrame;
}

1条回答
Fickle 薄情
2楼-- · 2019-09-16 11:26

There is a problem on the calculation, try this instead,

    - (void)keyboardWasShown:(NSNotification *)notification {
        CGRect viewFrame = input.frame;
        CGFloat textEndCord = CGRectGetMaxY(input.frame);
        CGFloat kbStartCord = textEndCord - ([[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]).size.height;
        viewFrame.size.height = kbStartCord;
        input.frame = viewFrame;
    }

Edited

General formula also for also supporting Landscape mode

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

    CGFloat keyboardHeight;
    CGRect viewFrame = textView.frame;
    CGFloat textMaxY = CGRectGetMaxY(textView.frame);
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
        keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.width;
    } else {
        keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;
    }
    CGFloat maxVisibleY = self.view.bounds.size.height - keyboardHeight;
    viewFrame.size.height = viewFrame.size.height - (textMaxY - maxVisibleY);
    textView.frame = viewFrame;
}

I had to add the UIInterfaceOrientationIsLandscape conditional since [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height; doesn't work when device is on Landscape. I know it's a little tricky, the other way of going around this would be detecting device rotation and changing a parameter's value. It is up to you.

Formula explanation

enter image description here

查看更多
登录 后发表回答