我有一個UIToolbar
,我編程創建了一個UITextField
和UIButton
添加到它作爲單獨的子視圖。下面的代碼顯示了創建UIToolbar
並返回UIView
的方法。IOS - UITextField在工具欄內的問題
- (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
單擊文本字段時沒有原始文本字段。另外,如何禁用文本字段中文本的編輯?我嘗試禁用用戶交互,但這使得文本字段不再可點擊。
爲什麼要這樣做? – Wain
文本字段應該是靜態的,文本只在用戶單擊文本字段時纔會更改,鍵盤用允許用戶輸入文本的工具欄打開。當用戶完成時,他們按下工具欄上的按鈕,並更新文本字段中的文本。 @Wain – ScottOBot