web-dev-qa-db-ja.com

プログラムでiPhoneキーボードの上にツールバーを配置する

場合によっては、iPhoneキーボードの上部にツールバーを追加したい(たとえば、フォーム要素をナビゲートしているときのiPhone Safariのように)。

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

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

94
Rob Drimmie

IOS 3.2以降、この効果を実現する新しい方法があります。

UITextFieldsおよびUITextViewsにはinputAccessoryViewプロパティがあり、任意のビューに設定できます。このプロパティは上に自動的に表示され、キーボードでアニメーション化されます。

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

142
tonklon

だから基本的に:

Initメソッドで:

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];
72
Josh

これは 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];
}

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

60
phi

キーボード通知、つまりUIKeyboardWillShowNotificationUIKeyboardWillHideNotificationなどに登録する場合、受け取る通知にはuserInfo dict(UIKeyboardBoundsUserInfoKeyにキーボードの境界が含まれます。 )。

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

24
amrox

3.0以降では、通知のuserInfo辞書からアニメーションの継続時間と曲線を取得できます。

たとえば、キーボードのスペースを確保するためにビューのサイズをアニメートするには、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に対して同様のアニメーションを実行します。

16
David Beck

このリンクは、inputaccesoryviewを段階的に理解するのに非常に役立つことがわかりました。

入力アクセサリビュー

3
Julian Osorio

このメソッドを作成し、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;
        }
}