IOS-工具栏内部UITextField的问题



我用程序创建了一个UIToolbar,其中添加了UITextFieldUIButton作为单独的子视图。下面的代码显示了创建UIToolbar并返回UIView的方法。

- (UIToolbar *)createToolbar
{
    //set background of toolbar
    UIImage *rawBackground = [UIImage imageNamed:@"toolbarBackground.png"];
    UIImage *toolBarBackground = [rawBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
    createPostBar = [[UIToolbar alloc] init];
    createPostBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
    [createPostBar setBackgroundImage:toolBarBackground forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    //add and syle all toolbar items
    textView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(5, 6, 270, 40)];
    textView.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
    textView.minNumberOfLines = 1;
    textView.maxNumberOfLines = 6;
    textView.returnKeyType = UIReturnKeyDefault;
    textView.font = [UIFont systemFontOfSize:15.0f];
    textView.textColor = [UIColor blackColor];
    textView.text = @"";
    textView.delegate = self;
    textView.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    textView.backgroundColor = [UIColor whiteColor];
    UIImage *postBtnBackground = [UIImage imageNamed:@"postButton.png"];
    UIImage *rawEntryBackground = [UIImage imageNamed:@"postFieldBackground.png"];
    UIImage *entryBackground = [rawEntryBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
    toolbarOverlay = [[UIImageView alloc] initWithImage:entryBackground];
    toolbarOverlay.frame = CGRectMake(4, 0, 278, 44);
    UIButton *postButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [postButton addTarget:self
                   action:@selector(btnEdit:)
         forControlEvents:UIControlEventTouchDown];
    [postButton setBackgroundImage:postBtnBackground forState:UIControlStateNormal];
    postButton.frame = CGRectMake(280, 5, 35.0, 35.0);
    [createPostBar addSubview:textView];
    [createPostBar addSubview:toolbarOverlay];
    [createPostBar addSubview:postButton];
    createPostBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    return createPostBar;
}

然后,当单击文本字段时,我将UIToolbar添加到UITextField中作为其附件视图。

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag <= 2)
    {
        textField.inputAccessoryView = [self createToolbar];
    }
}

单击文本字段时,键盘会弹出工具栏。现在对工具栏中的文本字段进行评级,允许您更改单击的文本字段的文本。现在对单击文本字段时光标停留在单击的文本字段中的情况进行评分。

当单击文本字段而没有原始文本字段时,如何使光标移动到工具栏中的UITextField。此外,如何禁用对文本字段中文本的编辑?我尝试禁用用户交互,但这使得文本字段不再可点击。

您可能不应该使用文本字段来显示数据,而是使用标签(带有手势识别器)。

要显示键盘,不要使用附件视图,只需将工具栏添加为子视图,并使文本字段成为第一个响应者。

如果你试图按照你描述的方式进行操作,它将不起作用,因为当原始文本字段不再是第一响应者时,附件视图将被删除。

最后,考虑使用带有文本字段的警报视图。。。