[英]Add subview to a custom class
I have a UITextField that I want to create a custom class on. 我有一个UITextField,我想创建一个自定义类。 So I created a file with a subclass of UITextField
. 所以我创建了一个带有UITextField
子类的文件。 Next, in the custom class, I want to implement a tableView
. 接下来,在自定义类中,我想实现一个tableView
。 Kind of like a auto-complete textField
. 有点像自动完成textField
。
I started creating it, and added the tableView
like this: 我开始创建它,并添加了tableView
如下所示:
[self addSubview:self.tableView];
When I run the app, the tableView
is in the textField
, so I can only see part of the tableView
. 当我运行应用程序时, tableView
位于textField
,因此我只能看到tableView
一部分。 How can I add it as a subview
so I can see the full tableView
? 如何将其添加为subview
以便我可以看到完整的tableView
?
This is what you are looking for https://github.com/gaurvw/MPGTextField 这就是你要找的https://github.com/gaurvw/MPGTextField
This uitextfield subclass does what you want - it's builed for 'search' feature. 这个uitextfield子类可以满足您的需求 - 它可以用于“搜索”功能。 If you still want to use your own, add tableview not to uitextfield itself, but like 如果您仍想使用自己的,请将tableview添加到uitextfield本身,但是喜欢
[[self superview] addSubview:tableViewController.tableView];
EDIT: 编辑:
you can set frame as: 你可以将框架设置为:
CGRect frameForPresentation = [self frame];
frameForPresentation.origin.y += self.frame.size.height;
frameForPresentation.size.height = 200;
[tableViewController.tableView setFrame:frameForPresentation];
The way to add subview to uitextfield is to overload layoutSubviews method and init your tableview there: 将子视图添加到uitextfield的方法是重载layoutSubviews方法并在那里初始化你的tableview:
- (void)layoutSubviews
{
[super layoutSubviews];
if (!self.tableview.superview)
{
[self setupView];
}
}
This will add the tableView
as the subView of the textField
. 这将把tableView
添加为textField
的subView。
self.tableView.frame = CGRectMake(0, CGRectGetHeight(self.bounds), CGRectGetWidth(self.bounds), YOUR_TABLE_HEIGHT);
[self addSubview:self.tableView];
self.clipsToBounds = NO;
However, a better way is to make the tableView
as the textField
's superView's subView, that is, the textField
and the tableView
should be siblings. 但是,更好的方法是将tableView
作为textField
的superView的subView,即textField
和tableView
应该是兄弟姐妹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.