0

我有一个简单的 UIViewController,其中包含一些对象:一个 UITextView,以及一个作为 InputAccessoryView 添加到 UTextView 的 UIToolBar。当 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]];
    }
}
4

1 回答 1

1

您在创建键盘附件视图时正在创建文本视图?我不确定有两个目的不明确的文本视图是否明智。

我确信这是一个疏忽。您的文本视图应该已经存在。为什么要一次又一次地创建 vewTxt?您可以设置框架,但您当然不应该创建一个新框架。

于 2013-08-15T19:33:09.657 回答