UITextView没有随着键盘显示调整大小

时间:2011-01-15 17:38:45

标签: iphone keyboard resize uitextview

我有一个文本视图(nib-file),UITextView最大化了整个视图。

当我将视图控制器推到导航控制器上时,我在文本视图的viewDidLoad方法中加载键盘。

我的问题是,当我写的文字比空格更多时,文字就在键盘后面。我按照了Apple here的样品。

如果我记录“newTextViewFrame”它是NULL。但如果我调试它的东西它有一个很好的价值。 即使我对其进行硬编码,UITextView也会改变大小。

这是我的代码:

- (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);

}

我的任务很简单:使用UITextView实现视图并打开键盘,并在文本超出时滚动UITextView。

请建议......

1 个答案:

答案 0 :(得分:1)

行上的keyboardTop后面有一个分号

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

这肯定会阻止newTextViewFrame获得调整后的高度。