开发者

UITextView not resizing along woth keyboard showing

开发者 https://www.devze.com 2023-02-04 04:40 出处:网络
I have a text view (nib-file) with a UITextView maximised the entire view. When ever i push the viewcontroller on to the navigationcontroller i load the keyboard in the text view\'s viewDidLoad metho

I have a text view (nib-file) with a UITextView maximised the entire view.

When ever i push the viewcontroller on to the navigationcontroller i load the keyboard in the text view's viewDidLoad method.

My problem is that when i write text that has more lines than there is space for, the text is behind the keyboard. I followed the sampe from Apple here.

If i log the "newTextViewFrame" it's NULL. but if i debug the thing it has a fine va开发者_开发问答lue. Even if i hardcode it, the UITextView doesent change size.

Here is my code:

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

/*
 Reduce the size of the text view so that it's not obscured by the keyboard.
 Animate the resize so that it's in sync with the appearance of the keyboard.
 */

NSDictionary *userInfo = [notification userInfo];

// Get the origin of the keyboard when it's displayed.
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
CGRect keyboardRect = [aValue CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.view.bounds;
newTextViewFrame.size.height = keyboardTop; - (self.view.bounds.origin.y);

// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];

// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];

createListingTextView.frame = newTextViewFrame;

[UIView commitAnimations];
NSLog(@"Textview resized: %@", newTextViewFrame);

}

My task i simple: Implement a View with a UITextView and the keyboard open and scroll the UITextView when ever the text exceeds.

Please advice...


There is a semi-colon after keyboardTop on the line

newTextViewFrame.size.height = keyboardTop; - (self.view.bounds.origin.y);

This would certainly prevent newTextViewFrame from getting the adjusted height.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号