iOS 8 キーボードでテキストビューが隠れる 質問する

iOS 8 キーボードでテキストビューが隠れる 質問する

UIViewController使用していますがself.accountViewController.modalPresentationStyle = UIModalPresentationFormSheet;、iOS 8 では最後にUITextView押したときにキーボードから隠れてしまいます。どうすれば回避できますか?

ベストアンサー1

@Oleg の解決策は優れていますが、キーボードがすでに表示されているときにサイズが変わることを考慮していません。また、アニメーションの期間と他のいくつかのパラメータがハードコードされています。以下の私の解決策を参照してください。

UIKeyboardWillChangeFrameNotificationのオブザーバーを追加するviewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];

そして、次のメソッドを追加します。

- (void)keyboardFrameWillChange:(NSNotification *)notification
{
    CGRect keyboardEndFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect keyboardBeginFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    UIViewAnimationCurve animationCurve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
    NSTimeInterval animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] integerValue];

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

    CGRect newFrame = self.view.frame;
    CGRect keyboardFrameEnd = [self.view convertRect:keyboardEndFrame toView:nil];
    CGRect keyboardFrameBegin = [self.view convertRect:keyboardBeginFrame toView:nil];

    newFrame.origin.y -= (keyboardFrameBegin.origin.y - keyboardFrameEnd.origin.y);
    self.view.frame = newFrame;

    [UIView commitAnimations];
}

viewDidUnload と Dealloc の両方でキーボード オブザーバーを削除することを忘れないでください。

おすすめ記事