0

当键盘出现时,我正在尝试调整 UITextView 的大小,但我不知道我做错了什么。

当键盘出现时,我调用了这三行代码:

CGSize kbSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

textView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-kbSize.height);

NSLog(@"%f,%f,%f,%f", textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, textView.frame.size.height);

由于某些非常奇怪的原因,我的键盘没有出现,这是记录的内容:

0.000000,-180.000000,480.000000,180.000000

为什么 CGRects y 原点在硬编码为 0 时设置为 -180?

编辑: textView 是我以编程方式创建的 UITextView。我没有使用界面生成器。

EDIT2:似乎问题在于视图在旋转时会自动调整大小。

4

1 回答 1

1

你可以试试这个:

- (void) moveTextViewForKeyboard:(NSNotification*)aNotification up:(BOOL)up {
    NSDictionary* userInfo = [aNotification userInfo];
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardEndFrame;

    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:animationCurve];

    CGRect newFrame = textView.frame;
    CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
    keyboardFrame.size.height -= tabBarController.tabBar.frame.size.height;
    newFrame.size.height -= keyboardFrame.size.height * (up?1:-1);
    textView.frame = newFrame;

    [UIView commitAnimations];   
}

希望它会有所帮助

于 2012-07-17T03:32:22.623 回答