Ios 以编程方式对齐iPhone键盘顶部的工具栏
在一些情况下,我希望在iPhone键盘的顶部添加一个工具栏(例如,在iPhone Safari中导航表单元素时) 目前,我正在使用常量指定工具栏的矩形,但由于界面的其他元素处于通量中-工具栏和导航栏位于屏幕顶部-每次我们对界面进行微小更改时,工具栏都会失去对齐 有没有办法通过编程确定键盘相对于当前视图的位置?没有办法(AFAIK)获取键盘视图的尺寸。然而,它是不变的,至少到目前为止,在每个iPhone版本中都是如此 如果将工具栏位置计算为相对于视图底部的偏移量,并考虑视图的大小,则不必担心是否存在导航栏 例如 您可以轻松创建一个Ios 以编程方式对齐iPhone键盘顶部的工具栏,ios,iphone,cocoa-touch,keyboard,Ios,Iphone,Cocoa Touch,Keyboard,在一些情况下,我希望在iPhone键盘的顶部添加一个工具栏(例如,在iPhone Safari中导航表单元素时) 目前,我正在使用常量指定工具栏的矩形,但由于界面的其他元素处于通量中-工具栏和导航栏位于屏幕顶部-每次我们对界面进行微小更改时,工具栏都会失去对齐 有没有办法通过编程确定键盘相对于当前视图的位置?没有办法(AFAIK)获取键盘视图的尺寸。然而,它是不变的,至少到目前为止,在每个iPhone版本中都是如此 如果将工具栏位置计算为相对于视图底部的偏移量,并考虑视图的大小,则不必担心是否存
keyboardHeight
函数,根据键盘是否显示返回大小,而不是定义,并将此工具栏定位移动到一个单独的函数中,以重新组织布局
此外,它还取决于您在何处进行此定位,因为视图的大小可能会根据导航栏设置在加载和显示之间发生变化。我相信最好的地方是在视图中显示。没有办法(AFAIK)获得键盘视图的尺寸。然而,它是不变的,至少到目前为止,在每个iPhone版本中都是如此
如果将工具栏位置计算为相对于视图底部的偏移量,并考虑视图的大小,则不必担心是否存在导航栏
例如
您可以轻松创建一个keyboardHeight
函数,根据键盘是否显示返回大小,而不是定义,并将此工具栏定位移动到一个单独的函数中,以重新组织布局
此外,它还取决于您在何处进行此定位,因为视图的大小可能会根据导航栏设置在加载和显示之间发生变化。我相信最好的地方是在视图中出现。如果您注册键盘通知,即
UIKeyboardWillShowNotification
UIKeyboardWillHideNotification
等,您收到的通知将包含userInfo
目录中的键盘边界(UIKeyboardBoundsUserInfoKey
)
请参阅
ui窗口
类参考。如果您注册键盘通知,即ui键盘将显示通知
ui键盘将隐藏通知
等,您收到的通知将包含userInfo
dict(ui键盘边界SUSERINFOKEY
)中的键盘边界
请参见ui窗口
类参考。因此基本上:
在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];
所以基本上:
在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];
在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];
}
在3.0及更高版本中为
UIKeyboardWillHideNotification
执行类似的动画。您可以从通知的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];
}
从iOS 3.2开始,有一种新方法可以实现此效果:
UITextFields
和UITextViews
具有一个inputAccessoryView
属性,您可以将其设置为任何视图,该属性自动显示在上方并使用键盘设置动画
请注意,您使用的视图不应位于其他位置的视图层次结构中,也不应将其添加到某个superview中,这是为您完成的。从iOS 3.2开始,有一种新方法可以实现此效果:
UITextFields
和UITextViews
具有一个inputAccessoryView
属性,您可以将其设置为任何视图,该属性自动显示在上方并使用键盘设置动画
请注意,您使用的视图不应位于其他位置的视图层次结构中,也不应将其添加到某个superview中,这是为您完成的。这是基于-我只是添加一个代码片段,在键盘顶部显示一个半透明的黑色工具栏,以及右侧的“完成”按钮:
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];
而-辞职键盘
看起来像:
-(void)resignKeyboard {
[aTextField resignFirstResponder];
}
希望这对某人有所帮助。这是基于-我只是添加了一个代码片段,在键盘顶部显示一个半透明的黑色工具栏,在右侧还有一个“完成”按钮:
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];
而-辞职键盘
看起来像:
-(void)resignKeyboard {
[aTextField resignFirstResponder];
}
希望这对某人有所帮助。创建此方法并在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;
}
}
创建此方法并在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;
}
}
这非常有效,谢谢!到目前为止,我一直在UIKeyboardDidShowNotification触发的选择器中进行此计算。我只在几个地方进行了测试,但它看起来是一个不错的点。从5.0开始,键盘大小不再是静态的。这非常有效,谢谢!到目前为止,我一直在UIKey触发的选择器中进行此计算B