0

キーボードが表示されたときにUITextViewのサイズを変更しようとしていますが、何が間違っているのかわかりません。

キーボードが表示されたときに呼び出されるコードは次の3行です。

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です。私はInterfaceBuilderを使用しませんでした。

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 に答える