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:UIKeyboardAnimationCur开发者_开发知识库veUserInfoKey] 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];
}
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
- (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];
}
And its all done...
精彩评论