3

これは非常に新人の質問です。下部にUIToolBarがあり、UIKeyBoardが表示されたときにキーボードでアニメーション的に上下に移動することになっています。私はUIKeyBoard通知の助けを借りてそれを動作させました。ここで説明しているビューでは、分割ビューが有効になっています。デバイスの向きが横向きの場合、列として両方のビューが表示されます[意味のあることを願っています]。

キーボードが表示されたら、これを行います

CGSize keyBoardSize = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;

CGRect toolbarFrame= [BottomToolBar frame];
toolbarFrame.origin.y -= keyBoardSize.height;    
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
BottomToolBar .frame = viewFrame;
[UIView commitAnimations];

キーボードが隠されているとき私はこれをします

toolbarFrame.origin.y += keyBoardSize.height;

私の問題は、デバイスの向きが横向きに変わったとき、キーボードが表示されたときに下部のツールバーがなくなったことです。すぐに上がるのがわかります。これを修正する方法がわかりません。誰か助けてもらえますか?また、分割ビューの両方のビューにキーボードをまたがらないようにする方法はありますか?

4

6 に答える 6

10

私もこの問題を抱えています。考えられるのは、キーボードを閉じて再表示することだけです(辞任してから、最初のレスポンダーに戻ります)。しかし、それは非常に不満足なようです。

また、rectを画面座標からビューの座標に変換する必要があることに注意してください。(画面座標は回転しません。)

CGRect keyboardRect = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
keyboardRect = [[BottomToolBar superview] convertRect:keyboardRect fromView:nil];

更新: UIKeyboardWillShowNotification に登録する必要があります。その後、インターフェイスが回転したときにアクションが呼び出されます:)

参照: https://devforums.apple.com/message/181482#181482

于 2010-03-26T05:52:15.320 に答える
6
- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)aNotification 
{
    CGRect keyboardBounds;
    [[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];

    CGFloat keyboardHeight;
    switch ([UIApplication sharedApplication].statusBarOrientation) {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            keyboardHeight = keyboardBounds.size.height;
            break;
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            keyboardHeight = keyboardBounds.size.width;
            break;
    }

    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    CGRect rect = table.frame;
    rect.size.height -= keyboardHeight;
    [UIView beginAnimations:@"ResizeForKeyboardShow" context:nil];
    [UIView setAnimationDuration:animationDuration];
    table.frame = rect;
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)aNotification
{
    CGRect keyboardBounds;
    [[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];

    CGFloat keyboardHeight;
    switch ([UIApplication sharedApplication].statusBarOrientation) {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            keyboardHeight = keyboardBounds.size.height;
            break;
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            keyboardHeight = keyboardBounds.size.width;
            break;
    }

    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    CGRect rectTable = table.frame;
    rectTable.size.height += keyboardHeight;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    table.frame = rectTable;
    [UIView commitAnimations];
}
于 2010-09-20T09:55:29.417 に答える
4

UITextFields には、UIKeyboard の上に UIToolBar を追加できる inputAccessoryView プロパティがあります。

于 2011-02-21T16:52:00.173 に答える
2
- (void)viewDidLoad { // Or somewhere else
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbDidChange:) UIKeyboardDidChangeFrameNotification object:nil];
}

- (void)kbDidChange:(NSNotification *)notification {
    NSDictionary* keyboardInfo = [notification userInfo];
    CGRect keyboardFrame = [[keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardFrame = [[messageBar superview] convertRect:keyboardFrame fromView:nil];

    CGRect ol = messageBar.frame; // messageBar is your UIToolbar..
    ol.origin.y = keyboardFrame.origin.y-44;
    messageBar.frame = ol;
}
于 2012-09-02T12:54:22.120 に答える
0

外付けキーボードを除いて、これらの回答はうまく機能します。ハードウェア キーボードが存在する場合、メッセージで渡されたキーボード フレームの「高さ」プロパティは、仮想キーボードの場合と同じであるように見えます。つまり、このコードは、非既存のキーボード (ぎこちないスペースの作成)。

私が見つけた最善の解決策は、キーボード フレームの "y" プロパティが画面の下部にあるように指定されていることに注意することです (仮想キーボードが実際に存在し、画面のすぐ外にあることを意味します)。

于 2010-12-31T14:53:16.530 に答える
0

UIViewAnimationOptionBeginFromCurrentState解決策は、ブロック ベースのアニメーションのオプションを設定することです。

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:[notificationInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
[UIView setAnimationBeginsFromCurrentState:YES];
BottomToolBar.frame = viewFrame;
[UIView commitAnimations];

あなたの場合。これにより、向きを縦向きから横向きに変更しても、ビューが突然動くことはありません。

于 2012-05-31T16:49:15.890 に答える