在某些情况下,我想将工具栏添加到iPhone键盘的顶部(例如,在iPhone Safari中,当您导航表单元素时)。
目前我正在用常量指定工具栏的矩形,但是因为界面的其他元素都在通量中--屏幕顶部的工具栏和导航栏--每次我们对界面做一个小的改变,工具栏就会失去对齐。
有没有办法以编程方式确定键盘相对于当前视图的位置?
发布于 2010-07-24 02:15:25
从iOS 3.2开始,有了一种新的方法来实现这种效果:
UITextFields
和UITextViews
有一个inputAccessoryView
属性,您可以将其设置为任何视图,该属性会自动显示在上方,并使用键盘设置动画。
请注意,您使用的视图不应该在其他地方的视图层次结构中,也不应该将其添加到某个superview中,这是为您完成的。
发布于 2008-12-28 05:37:29
所以基本上:
在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];
发布于 2011-05-17 03:52:33
这是基于existing answer from 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];
}
希望这能帮助到一些人。
https://stackoverflow.com/questions/158574
复制相似问题