Add subview to custom class

I have a UITextField that I want to create my own class for. So I created a file with a subclass UITextField

. Then in a custom class, I want to implement tableView

. A kind of autocomplete textField

.

I started building it and added it tableView

like this:

[self addSubview:self.tableView];

      

When I run the application, it tableView

is in textField

, so I can only see a part tableView

. How can I add it like subview

to see the complete one tableView

?

+3


source to share


2 answers


This is what you are looking for https://github.com/gaurvw/MPGTextField

This uitextfield subclass does what you want - it's built for the search function. If you still want to use your own, add the tableview not to the uitextfield itself, but like

[[self superview] addSubview:tableViewController.tableView];

      

EDIT:



you can set the frame like:

 CGRect frameForPresentation = [self frame];
 frameForPresentation.origin.y += self.frame.size.height;
 frameForPresentation.size.height = 200;
 [tableViewController.tableView setFrame:frameForPresentation];

      

The way to add a subview to a uitextfield is to overload the layoutSubviews method and create it:

- (void)layoutSubviews 
{ 
[super layoutSubviews]; 
if (!self.tableview.superview) 
{ 
[self setupView]; 
} 
}

      

+1


source


This will add tableView

both subView textField

.

self.tableView.frame = CGRectMake(0, CGRectGetHeight(self.bounds), CGRectGetWidth(self.bounds), YOUR_TABLE_HEIGHT);
[self addSubview:self.tableView];
self.clipsToBounds = NO;

      



However, the best way is to do tableView

both subView paging textField

, i.e. textField

and tableView

must be brothers and sisters.

0


source







All Articles