2010-05-13 53 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