[英]Programmatically align a toolbar on top of the iPhone keyboard
在某些情况下,我想在 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];
}
希望能对某人有所帮助。
如果您注册键盘通知,即UIKeyboardWillShowNotification
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版本中,它都是恒定的。
如果将工具栏位置计算为相对于视图底部的偏移量,并且考虑了视图的大小,则不必担心导航栏是否存在。
例如
#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中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.