这是一个非常常见的主题,并且有许多答案。
情况:我有一个全屏(减号工具栏)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];
}
答案 0 :(得分:3)
尝试将-becomeFirstResponder
来电移至-viewWillAppear:animated:
或-viewDidAppear:animated:
。我认为-viewDidLoad
通常在视图实际添加到视图层次结构之前调用。
答案 1 :(得分:0)
在您的代码中添加此内容
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
return YES;
}
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.barStyle = UIBarStyleBlack;
keyboardDoneButtonView.translucent = YES;
keyboardDoneButtonView.tintColor = nil;
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneBtnTapped:)];
// I put the spacers in to push the doneButton to the right side of the picker view
UIBarButtonItem *spacer1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
// I put the spacers in to push the doneButton to the right side of the picker view
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:spacer, spacer1, doneButton, nil]];
// Plug the keyboardDoneButtonView into the text field...
textView.inputAccessoryView = keyboardDoneButtonView;
return YES;
}
- (void)doneBtnTapped:(id)sender {
[yourTextView resignFirstResponder];
}
这一切都完成了......