94

いくつかのケースでは、iPhoneキーボードの上部にツールバーを追加したいと思います(たとえば、フォーム要素をナビゲートしているときのiPhone Safariのように)。

現在、定数を使用してツールバーの長方形を指定していますが、インターフェイスの他の要素(画面上部のツールバーとナビゲーションバー)が流動的であるため、インターフェイスを少し変更するたびに、ツールバーの位置がずれます。

現在のビューに対するキーボードの位置をプログラムで決定する方法はありますか?

4

7 に答える 7

141

iOS 3.2 では、この効果を実現する新しい方法があります。

UITextFieldsまた、任意のビューに設定できるプロパティがあり、自動的に上に表示され、キーボードでアニメーション化されますUITextViewsinputAccessoryView

使用するビューは、他のビュー階層にあるべきではなく、スーパービューに追加するべきではないことに注意してください。これは自動的に行われます。

于 2010-07-23T18:15:25.043 に答える
72

だから基本的に:

初期化メソッドでは:

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

次に、バーの位置を調整するために上記のメソッドを使用します。

-(void) keyboardWillShow:(NSNotification *) note
{
    CGRect r  = bar.frame, t;
    [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
    r.origin.y -=  t.size.height;
    bar.frame = r;
}

でラップして位置の変更をアニメーション化することで、きれいにすることができます

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
//...
    [UIView commitAnimations];
于 2008-12-28T05:37:28.760 に答える
60

これは、tonklonからの既存の回答に基づいています。キーボードの上部に半透明の黒いツールバーと右側の「完了」ボタンを表示するコードスニペットを追加しています。

UIToolbar *toolbar = [[[UIToolbar alloc] init] autorelease];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
[toolbar sizeToFit];

UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignKeyboard)];

NSArray *itemsArray = [NSArray arrayWithObjects:flexButton, doneButton, nil];

[flexButton release];
[doneButton release];
[toolbar setItems:itemsArray];

[aTextField setInputAccessoryView:toolbar];

とのように-resignKeyboard見えます:

-(void)resignKeyboard {
  [aTextField resignFirstResponder];
}

それが誰かを助けることを願っています。

于 2011-05-16T19:52:32.597 に答える
24

キーボード通知などに登録する場合、受信する通知には、 dict( )UIKeyboardWillShowNotification UIKeyboardWillHideNotification内のキーボードの境界が含まれます。userInfoUIKeyboardBoundsUserInfoKey

UIWindowクラスリファレンスを参照してください。

于 2008-10-01T18:35:48.993 に答える
16

userInfo3.0 以降では、通知の辞書からアニメーションの長さと曲線を取得できます。

たとえば、ビューのサイズをアニメートしてキーボード用のスペースを確保するには、 に登録しUIKeyboardWillShowNotificationて次のようにします。

- (void)keyboardWillShow:(NSNotification *)notification
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];

    CGRect frame = self.view.frame;
    frame.size.height -= [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
    self.view.frame = frame;

    [UIView commitAnimations];
}

に対して同様のアニメーションを実行しUIKeyboardWillHideNotificationます。

于 2010-01-10T23:34:45.507 に答える
0

このメソッドを作成し、ViewWillLoad で呼び出します。

        - (void) keyboardToolbarSetup
{
    if(self.keyboardToolbar==nil)
        {
        self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];

        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(anyAction)];

        UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(anyOtherAction)];


        NSArray *toolbarButtons = [[NSArray alloc]initWithObjects:cancelButton,extraSpace,doneButton, nil];

        [self.keyboardToolbar setItems:toolbarButtons];

        self.myTextView.inputAccessoryView=self.keyboardToolbar;
        }
}
于 2014-12-11T16:33:47.153 に答える
-3

キーボード ビューの寸法を取得する方法 (AFAIK) はありません。ただし、少なくともこれまでのすべての iPhone バージョンでは一定です。

ツールバーの位置をビューの BOTTOM からのオフセットとして計算し、ビューのサイズを考慮に入れる場合、ナビゲーション バーが存在するかどうかを心配する必要はありません。

例えば

#define KEYBOARD_HEIGHT 240 // example - can't remember the exact size
#define TOOLBAR_HEIGHT 30

toolBarRect.origin.y = viewRect.size.height - KEYBOARD_HEIGHT - TOOLBAR_HEIGHT;

// move toolbar either directly or with an animation

keyboardHeight定義の代わりに、キーボードが表示されているかどうかに基づいてサイズを返す関数を簡単に作成し、このツールバーの配置をレイアウトを再編成する別の関数に移動できます。

また、ナビゲーションバーの設定に基づいて、ビューのサイズが読み込まれるか表示されるかで変わる可能性があるため、この配置をどこで行うかによっても異なります。それを行うのに最適な場所はviewWillAppearだと思います。

于 2008-10-01T17:28:51.167 に答える