将子视图添加到 UITextField leftView 属性
我正在尝试为 iPhone/iPad 实现自动完成文本字段。我已经对 UITextField 进行了子类化。当用户输入搜索条件时,如果搜索字符串与我们数据库中的某个实体匹配,则该实体将成为用户搜索条件的一部分,并且我想将代表该实体的按钮附加到文本字段的 leftView 属性。这样,当用户输入更多搜索条件时,项目就会附加到左侧视图,这与邮件编辑器在“收件人/抄送”字段中输入联系人时的方式类似。
我的问题是 leftView 没有显示任何内容。我可以通过设置 leftView 的框架属性来增加 leftView 的大小。但是我添加的任何子视图(例如 UIImageView 或 UIButton)不会显示在文本字段的左侧视图中。当我向左视图添加子视图并设置左视图的框架时,文本字段的光标确实移动到右侧,但没有任何东西可见。
我创建一个新的 UIView 并将 leftView 设置为该视图。
然后我创建新的 UIButton 对象并将它们作为子视图添加到 leftView。
我在layoutSubviews中设置了按钮的框架和左视图。
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];
}
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.
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.
I create a new UIView and set the leftView to that view.
Then I create new UIButton objects and add those as subviews to the leftView.
I set the frames of the buttons and the leftview in layoutSubviews.
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).
There must be something about leftView that I dont understand.
Here is code snippet:
// 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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了。我没有在layoutSubviews覆盖中调用[superlayoutSubviews]。现在它可以正确渲染左视图的子视图。之前,我只是在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.