键盘顶部的工具栏和firstresponder
这是一个非常常见的话题,并且有很多答案。
情况:我有一个全屏(不包括工具栏)UITextView,底部有一个UIToolbar。当 UITextView 获得第一响应者时,我想让工具栏随键盘向上滑动,并添加一个“完成”按钮,该按钮将关闭键盘。
到目前为止:我已经完全正常工作了,基于这个例子。除了当我将 [textViewBecomeFirstResponder];
放入 viewDidLoad
中时,工具栏不会显示动画。即使 keyboardWillShow
被调用。有人知道吗?
代码:这样您就不必查看示例代码,这就是发生的情况:
在viewDidLoad中:
- (void)viewDidLoad {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[textView becomeFirstResponder];
[super viewDidLoad];
}
在keyboardWillShow中:
- (void)keyboardWillShow:(NSNotification *)notification {
NSLog(@"keyboard will show");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(keyboardDone)];
NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:[toolbar items]];
[toolbarItems addObject:doneButton];
[toolbar setItems:toolbarItems];
CGRect frame = self.view.frame;
frame.size.height -= [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
self.view.frame = frame;
[UIView commitAnimations];
}
This is a pretty common topic and has many answers out there.
Situation: I have a full screen (minus toolbar) UITextView with a UIToolbar at the bottom. when the UITextView gets first responder I want to have the toolbar slide up with the keyboard and add a "done" button which will dismiss the keyboard.
So far: I have this completely working, based on this example. Except for the fact that when I put [textView becomeFirstResponder];
in my viewDidLoad
then the toolbar doesn't animate. Even though keyboardWillShow
is called. Does anyone have any idea?
Code: Just so you don't have to check out the example code this is what is happening:
In viewDidLoad:
- (void)viewDidLoad {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[textView becomeFirstResponder];
[super viewDidLoad];
}
In keyboardWillShow:
- (void)keyboardWillShow:(NSNotification *)notification {
NSLog(@"keyboard will show");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(keyboardDone)];
NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:[toolbar items]];
[toolbarItems addObject:doneButton];
[toolbar setItems:toolbarItems];
CGRect frame = self.view.frame;
frame.size.height -= [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
self.view.frame = frame;
[UIView commitAnimations];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
-becomeFirstResponder
调用移至-viewWillAppear:animated:
或-viewDidAppear:animated:
。我认为-viewDidLoad
通常在视图实际添加到视图层次结构之前调用。Try moving the
-becomeFirstResponder
call to-viewWillAppear:animated:
or-viewDidAppear:animated:
. I think-viewDidLoad
is usually called just before the view's actually added to the view hierarchy.将其添加到您的代码中
并且一切都完成了......
Add this in your code
And its all done...