2010-05-13 56 views
0

我正在嘗試爲iPhone/iPad實現自動填充文本字段。我有一個UITextField子類。當用戶輸入搜索條件時,如果搜索字符串與我們數據庫中的某個實體相匹配,那麼該實體就成爲用戶搜索條件的一部分,並且我想將表示該實體的按鈕附加到文本字段的leftView屬性中。通過這種方式,當用戶輸入更多的搜索條件時,項目將被添加到左側視圖中,與將聯繫人輸入到/ cc字段時的郵件編輯器類似。將子視圖添加到UITextField leftView屬性

我的問題是,leftView不顯示任何東西。我可以通過在leftView上設置frame屬性來增加leftView的大小。但是,我添加的任何子視圖(例如UIImageView或UIButton)不顯示在文本字段的左側視圖中。當我向左視圖添加子視圖並設置左視圖的框架時,文本字段的光標確實向右移動,但沒有任何可見的內容。

我創建一個新的UIView並將leftView設置爲該視圖。

然後,我創建新的UIButton對象,並將它們作爲子視圖添加到leftView。

我在layoutSubviews中設置了按鈕的框架和leftview。

leftView的大小肯定會增加,而文本字段光標會按照它應該的方向向右移動。

但是,沒有一個按鈕顯示(不可見)。

必須有一些關於leftView的東西,我不明白。

下面是代碼片段:

// add search criteria to the left view 
// this will create a button on the left side of the text field 
(void) addSubValue:(NSString*) value display:(NSString*)display 
{ 
    if(subValues==nil) 
    { 
     NSMutableArray * tmp=[[NSMutableArray alloc] init]; 

     self.subValues=tmp; 

     [tmp release]; 
    } 

    AutocompleteSubValue * subValue =[[AutocompleteSubValue alloc] init]; 

    subValue.value=value; 
    subValue.display=display; 

    UIButton * button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 

    button.frame=CGRectMake(0, 0, 100, 30); // this frame gets reset later in layoutSubviews... 
    button.titleLabel.text=display; 
    button.titleLabel.textColor=[UIColor whiteColor]; 
    button.titleLabel.backgroundColor=[UIColor blueColor]; 
    button.backgroundColor=[UIColor blueColor]; 

    [button addTarget:self action:@selector(touchSubValue:) forControlEvents:UIControlEventTouchUpInside]; 

    subValue.view=button; 

    [self.leftView addSubview:button]; 


    [subValues addObject:subValue]; 
    [subValue release]; 

    [self setNeedsLayout]; 
} 

(void) touchSubValue:(id)sender 
{ 
    UIButton * button=sender; 
    // find value user touched and remove from the search 
    AutocompleteSubValue * subValue; 
    for (AutocompleteSubValue * s in subValues) 
    { 
     if([s.view isEqual:button]) 
     { 
      subValue=s; 
      break; 
     } 
    } 
    if(subValue) 
    { 
     [button removeFromSuperview]; 
     [subValues removeObject:subValue]; 
    } 
} 

(void)layoutSubviews 
{ 
    if(self.leftView==nil) 
    { 
     UIView * view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)]; // this views frame gets reset in layoutSubviews 
     self.leftView = view; 
     self.leftViewMode = UITextFieldViewModeAlways; 
     [view release]; 
    } 

    CGFloat left=30; 

    // set frames of sub values 
    for (AutocompleteSubValue * subValue in subValues) 
    { 
     CGFloat display_width=100+16; // TODO: get from display text 

     CGFloat height=30; 

     CGRect frame=CGRectMake(left, 4, display_width, height); 

     subValue.view.frame=frame; 

     left+=display_width+10; 
    } 

    // set width of left view to make room for buttons, and move cursor to the right 
    self.leftView.frame=CGRectMake(self.leftView.frame.origin.x, self.leftView.frame.origin.y, left, self.leftView.frame.size.height); 

    [self.leftView layoutSubviews]; 
} 
+0

什麼是UIButton的框架?也許你應該顯示一些代碼,你是如何修改leftView的。 – kennytm 2010-05-13 20:51:28

+0

好的抱歉,我無法正確格式化代碼。它不斷創建水平滾動條,並打破格式化... – 2010-05-13 21:07:35

回答

1

我想通了這一點。我沒有在layoutSubviews覆蓋中調用[super layoutSubviews]。現在它正確地呈現左視圖的子視圖。之前,我只是在layoutSubview中調用了[leftView layoutSubviews],但沒有奏效。

+0

嗨,你能提供更多的代碼? 例如AutocompleteSubValue還是UITextView子類的工作實現? 我試圖得到類似的工作,但不能... 謝謝 – 2017-06-26 08:12:22