我有一个简单的UIViewController,其中包含一些对象:UITextView和UIToolBar,它作为InputAccessoryView添加到UTextView中。当调用viewDidLayoutSubviews时,我设置了一个通知观察器,当键盘出现时调用函数“keyboardDidShow”,这样我就可以调整UITextView的大小,使其不在键盘后面。当调用textViewShouldBeginEditing时,我将InputAccessoryView添加到UITextView。但是当我去解除视图时,会抛出一个未知错误。此外,一旦我单击UIToolBar中充当InputAccessoryView的UITextField,我就不能再返回编辑UITextView了。我的代码如下:
- (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]];
}
}
答案 0 :(得分:1)
创建键盘附件视图时是否正在创建文本视图?我不确定两个文本视图的目的不明确是明智的。
我确信这是一种疏忽。您的文本视图应该已经存在。为什么要一次又一次地创建vewTxt?您可以设置框架,但您当然不应该创建新框架。