2013-07-26 46 views
1

我有一個UIToolbar,我編程創建了一個UITextFieldUIButton添加到它作爲單獨的子視圖。下面的代碼顯示了創建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; 
} 

我再加入UIToolbarUITextField作爲其附件視圖單擊文本字段時。

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

當文本字段被點擊時,鍵盤彈出工具欄。現在評價工具欄中的文本字段允許您更改被點擊的文本字段的文本。單擊文本字段時光標停留在現在的速率。

如何使光標移動到工具欄中的UITextField單擊文本字段時沒有原始文本字段。另外,如何禁用文本字段中文本的編輯?我嘗試禁用用戶交互,但這使得文本字段不再可點擊。

+0

爲什麼要這樣做? – Wain

+0

文本字段應該是靜態的,文本只在用戶單擊文本字段時纔會更改,鍵盤用允許用戶輸入文本的工具欄打開。當用戶完成時,他們按下工具欄上的按鈕,並更新文本字段中的文本。 @Wain – ScottOBot

回答

1

您可能不應該使用文本字段來顯示數據,而是使用標籤(使用手勢識別器)。

要顯示您的鍵盤,請不要使用附件視圖,只需將工具欄添加爲子視圖並使文本字段成爲第一響應者。

如果您嘗試這樣做,您描述的方式將無法使用,因爲當原始文本字段不再是第一個響應者時,附件視圖將被刪除。

最後,想想也許使用帶有文本字段的警報視圖來代替...

+0

我會嘗試使用標籤,並讓你知道它是否有效。另外,我如何將子視圖添加到鍵盤視圖? @Wain – ScottOBot

+0

不要將視圖添加爲鍵盤的子視圖。將它添加到與標籤相同的視圖中。或者使用警報視圖。 – Wain

+0

好吧,將它添加到視圖的底部,當鍵盤出現時,它會向上推工具欄。當它消失時,我可以正確刪除工具欄? @Wain – ScottOBot