将子视图添加到UITextField leftView属性 [英] Adding subviews to UITextField leftView property

查看:153
本文介绍了将子视图添加到UITextField leftView属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为iPhone / iPad实现自动填充文本字段。我已经将UITextField子类化了。当用户输入搜索条件时,如果搜索字符串与我们数据库中的某个实体匹配,那么该实体将成为用户搜索条件的一部分,并且我想将表示该实体的按钮附加到文本字段的leftView属性。这样,当用户输入更多搜索条件时,在将联系人输入到to / cc字段时,以与邮件编辑器类似的方式将项目附加到左视图。

I am trying to implement an autocomplete text field for the iPhone/iPad. I have subclassed a UITextField. As a user enters search criteria, if the search string matches some entity in our database, then that entity becomes part of the user's search criteria and I want to append a button representing that entity to the leftView property of the text field. That way as the user enters more search criteria, items are appended to the left view, in a similar way to the mail composer when entering contacts into the to/cc fields.

我的问题是leftView没有显示任何内容。我可以通过在leftView上设置frame属性来增加leftView的大小。但是我添加的任何子视图(例如UIImageView或UIButton)都不会显示在文本字段的左视图中。当我向左视图添加子视图并设置左视图的框架时,文本字段的光标确实向右移动,但没有任何内容可见。

My problem is that the leftView does not show anything. I can increase the size of the leftView by setting the frame property on the leftView. But any subViews I add (for example an UIImageView or a UIButton), do NOT show up in the left view of the text field. The cursor of the text field does move to the right as I add subviews to the left view and set the frame of the left view, but nothing is visible.

我创建一个新的UIView并将leftView设置为该视图。

I create a new UIView and set the leftView to that view.

然后我创建新的UIButton对象并将其作为子视图添加到leftView。

Then I create new UIButton objects and add those as subviews to the leftView.

我在layoutSubviews中设置了按钮和左视图的帧。

I set the frames of the buttons and the leftview in layoutSubviews.

leftView的大小明显增加,文本字段光标向右移动它应该。

The leftView definitely increases in size and the text field cursor moves to the right as it should.

但是,没有任何按钮显示(不可见)。

But, none of the buttons show up (the are not visible).

必须关于leftView,我不明白。

There must be something about leftView that I dont understand.

这是代码片段:

    // 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];
}


推荐答案

我想出来了。我没有在layoutSubviews覆盖中调用[super layoutSubviews]。现在它正确呈现了leftview的子视图。以前,我只是在layoutSubview中调用[leftView layoutSubviews]但是没有用。

I figured this out. I was not calling [super layoutSubviews] in the layoutSubviews override. Now it renders the subviews of the leftview correctly. Before, I was just calling [leftView layoutSubviews] in layoutSubview but that did not work.

这篇关于将子视图添加到UITextField leftView属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆