プログラムでiPhoneキーボードの上にツールバーを配置する
-
03-07-2019 - |
質問
いくつかの場合、iPhoneキーボードの上部にツールバーを追加したい(たとえば、フォーム要素をナビゲートしているときのiPhone Safariのように)。
現在、ツールバーの長方形を定数で指定していますが、インターフェイスの他の要素は流動的であるため-ツールバーと画面上部のナビゲーションバー-マイナーなインターフェイスを変更するたびに、ツールバーの位置がずれます。
現在のビューに関連してキーボードの位置をプログラムで決定する方法はありますか?
解決
iOS 3.2以降、この効果を実現する新しい方法があります:
UITextFields
および UITextViews
には inputAccessoryView
プロパティがあり、任意のビューに設定できます。このプロパティは上に自動的に表示され、キーボードでアニメーション化されます。
使用するビューは、他のビュー階層にあるべきではなく、スーパービューに追加するべきでもないことに注意してください。これはあなたのために行われます。
他のヒント
基本的に:
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];
これは、に基づいています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];
}
誰かを助ける希望。
キーボード通知、つまり UIKeyboardWillHideNotification
などに登録すると、受信した通知には userInfo
dict( UIKeyboardBoundsUserInfoKey
)。
UIWindow
クラスリファレンスを参照してください。
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
に対して同様のアニメーションを実行します。
このリンクは、inputaccesoryviewを段階的に理解するのに非常に役立つことがわかりました。
このメソッドを作成し、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バージョンで一定です。
ビューの下部からのオフセットとしてツールバーの位置を計算し、ビューのサイズを考慮する場合、ナビゲーションバーが存在するかどうかを心配する必要はありません。
E.g。
#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です。