2012-07-17 84 views
0

我想在键盘出现时调整我的UITextView,但我不知道我在做什么错误。设置UIView框架

我出现时的键盘这些三行代码名为:

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。我没有使用Interface Builder。

编辑2:看起来问题是视图旋转时自动调整大小。

回答

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]; 
} 

希望对大家有所帮助

+0

我已经解决了刚开始一个新的Xcode项目和略超过一点移动代码这个问题,但你的代码看起来不错过,所以有这个upvote! – jadengeller 2012-07-17 06:04:58