いくつかのケースでは、iPhoneキーボードの上部にツールバーを追加したいと思います(たとえば、フォーム要素をナビゲートしているときのiPhone Safariのように)。
現在、定数を使用してツールバーの長方形を指定していますが、インターフェイスの他の要素(画面上部のツールバーとナビゲーションバー)が流動的であるため、インターフェイスを少し変更するたびに、ツールバーの位置がずれます。
現在のビューに対するキーボードの位置をプログラムで決定する方法はありますか?
いくつかのケースでは、iPhoneキーボードの上部にツールバーを追加したいと思います(たとえば、フォーム要素をナビゲートしているときのiPhone Safariのように)。
現在、定数を使用してツールバーの長方形を指定していますが、インターフェイスの他の要素(画面上部のツールバーとナビゲーションバー)が流動的であるため、インターフェイスを少し変更するたびに、ツールバーの位置がずれます。
現在のビューに対するキーボードの位置をプログラムで決定する方法はありますか?
iOS 3.2 では、この効果を実現する新しい方法があります。
UITextFields
また、任意のビューに設定できるプロパティがあり、自動的に上に表示され、キーボードでアニメーション化されますUITextViews
。inputAccessoryView
使用するビューは、他のビュー階層にあるべきではなく、スーパービューに追加するべきではないことに注意してください。これは自動的に行われます。
だから基本的に:
初期化メソッドでは:
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];
これは、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];
}
それが誰かを助けることを願っています。
キーボード通知などに登録する場合、受信する通知には、 dict( )UIKeyboardWillShowNotification
UIKeyboardWillHideNotification
内のキーボードの境界が含まれます。userInfo
UIKeyboardBoundsUserInfoKey
UIWindow
クラスリファレンスを参照してください。
userInfo
3.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
ます。
このメソッドを作成し、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;
}
}
キーボード ビューの寸法を取得する方法 (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だと思います。