以编程方式在iPhone键盘上alignment工具栏

在几种情况下,我想在iPhone键盘的顶部添加一个工具栏(例如,在浏览表单元素的iPhone Safari中)。

目前,我正在用常量指定工具栏的矩形,但由于界面的其他元素处于stream动状态 – 屏幕顶部的工具栏和导航栏 – 每当我们进行小的界面更改时,工具栏都不alignment。

有没有一种方法来编程确定键盘相对于当前视图的位置?

从iOS 3.2开始,有一个新的方法可以达到这个效果:

UITextFieldsUITextViews有一个inputAccessoryView属性,您可以将其设置为任何视图,该视图自动显示在上方并使用键盘进行animation处理。

请注意,您使用的视图既不应该在其他地方的视图层次结构中,也不应将其添加到某个超级视图,这是为您完成的。

所以基本上:

在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现有的答案 – 我只是添加一个代码片段,在键盘上显示一个半透明的黑色工具栏,以及在右边的“完成”button:

 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]; } 

希望能帮助别人。

如果您注册键盘通知,即UIKeyboardWillShowNotification UIKeyboardWillHideNotification等,您收到的通知将包含userInfo字典( UIKeyboardBoundsUserInfoKey )中键盘的边界。

请参阅UIWindow类的参考。

在3.0及更高版本中,您可以从通知的userInfo字典中获取animation持续时间和曲线。

例如,为视图的大小设置animation,为键盘腾出空间,注册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做类似的animation。

我发现这个链接非常有用,可以逐步理解inputaccesoryview。

input配件视图

创build此方法并在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)无法获得键盘视图的尺寸。 不过至less在所有的iPhone版本中都是如此。

如果将工具栏位置计算为距视图底部的偏移量,并考虑视图大小,则不必担心导航栏是否存在。

例如

 #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 

您可以轻松创build一个keyboardHeight函数,根据是否显示keyboardHeight来返回大小,并将此工具栏定位移动到重新组织布局的单独函数中,而不是定义。

此外,它可以取决于你在哪里做这个定位,因为你的视图的大小可能会根据你的导航栏设置加载和显示之间的变化。 我相信最好的地方是可以看到的。