这是一个相当普遍的话题,并有很多答案。与第一响应者的键盘顶部的工具栏
情况:我有一个完整的屏幕(减去工具栏)UITextView UIToolbar在底部。当UITextView获得第一响应者时,我想让工具栏向上滑动键盘并添加一个“完成”按钮,它将关闭键盘。
到目前为止:我有这个完全工作,基于this example。除了当我将[textView becomeFirstResponder];
放入我的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];
}
你是了不起的!收藏此谢谢。它在-viewWillAppear中无效:动画:但它与-viewDidAppear一起使用:动画。我不知道那些加载的顺序,所以谢谢你今天教我新东西。 – RyanJM 2010-07-01 22:13:52