I have a simple UIViewController with a few objects in it: A UITextView, and a UIToolBar that gets added as a InputAccessoryView to the UTextView. When viewDidLayoutSubviews is called I set a notification observer to call a function "keyboardDidShow" when the keyboard appears so I can resize the UITextView so it's not behind the keyboard. and when textViewShouldBeginEditing is called I add the InputAccessoryView to the UITextView. But when I go to dismiss the view, a unknown error gets thrown. Also, Once I click on the UITextField in the UIToolBar that acts as the InputAccessoryView, I can no longer go back to edit the UITextView. My code is below:
- (void)viewDidLayoutSubviews {
if (!resized) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[poemView becomeFirstResponder];
}
}
- (void)keyboardDidShow:(NSNotification *)note {
if (!resized) {
NSDictionary *keyboardInfo = [note userInfo];
NSValue *keyboardFrameSize = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameSize CGRectValue];
toolBar.frame = CGRectMake(0, 0, toolBar.frame.size.width, toolBar.frame.size.height);
[txtView setFrame:CGRectMake(txtView.frame.origin.x, txtView.frame.origin.y, txtView.frame.size.width, txtView.frame.size.height - (keyboardFrameBeginRect.size.height))];
[txtView setContentSize:CGSizeMake(txtView.frame.size.width, txtView.frame.size.height - keyboardFrameBeginRect.size.height)];
[[NSNotificationCenter defaultCenter] removeObserver:self];
resized = YES;
}
}
- (IBAction)dismissView:(id)sender {
vewTxt = nil;
[txtView setInputAccessoryView:nil];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if (![txtView inputAccessoryView]) {
[txtView setInputAccessoryView:[self createInputAccessoryView]];
}
return YES;
}
- (UIToolbar *)createInputAccessoryView {
UIToolbar *acc = [[UIToolbar alloc] init];
[acc setBackgroundColor:[UIColor redColor]];
[acc setTintColor:[UIColor redColor]];
[acc sizeToFit];
[acc setFrame:CGRectMake(0,txtView.frame.size.height - 44, txtView.frame.size.width, 44)];
vewTxt = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, vewTxt.frame.size.width - 25, 25)];
[vewTxt setText:@"Etc...."];
[vewTxt setDelegate:self];
[vewTxt setFont:[UIFont italicSystemFontOfSize:14]];
[vewTxt setTextColor:[UIColor grayColor]];
[vewTxt setBorderStyle:UITextBorderStyleRoundedRect];
UIBarButtonItem *titleItem = [[UIBarButtonItem alloc] initWithCustomView:titleTxt];
NSArray *items = [NSArray arrayWithObjects:titleItem, nil];
[acc setItems:items animated:YES];
return acc;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if ([[textField text] isEqualToString:@"Etc...."]) {
[textField setTextColor:[UIColor blackColor]];
[textField setText:@""];
[textField setFont:[UIFont systemFontOfSize:14]];
}
}
You are creating a text view when you create the keyboard accessory view? I am not sure it is wise to have two text views with unclear purposes.
I am sure this is an oversight. Your text view should already exist. Why would you create vewTxt again and again? You can set the frame, but you certainly should not create a new one.