我有一个UIToolbar
我以编程方式创建的,它有一个UITextField
并UIButton
作为单独的子视图添加到它。下面的代码显示了创建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
到 a作为其附件视图。UITextField
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField.tag <= 2)
{
textField.inputAccessoryView = [self createToolbar];
}
}
单击文本字段时,会弹出带有工具栏的键盘。现在评价工具栏中的文本字段允许您更改单击的文本字段的文本。现在在单击文本字段时评分,光标停留在单击的文本字段中。
UITextField
当单击文本字段并且没有原始文本字段时,如何使光标移动到工具栏中的 。另外,如何禁用文本字段中文本的编辑?我尝试禁用用户交互,但这使得文本字段不再可点击。