I have UITableView
. I want to add a UITextField
above tableView, which could be accessible by pulling tableView down. And I want to hide my textField by pulling tableView up. How can I do this?
Here's what I tried:
[self.messagesTableView addSubview:self.messageField];
- (UITextField*)messageField
{
if (!_messageField)
{
_messageField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.messagesTableView.frame.size.width, kMessageFieldHeight)];
_messageField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_messageField.backgroundColor = [UIColor greenColor];
}
return _messageField;
}
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
if (scrollView == self.messagesTableView)
{
CGRect newFrame = self.messagesTableView.frame;
newFrame.origin.y = self.messagesTableView.contentOffset.y + kMessageFieldHeight;
self.messagesTableView.frame = newFrame;
}
}
I have done such kind of functionality in my application. What i did just follow the steps.
1) Add
one view to negative position of tableView
. Here in this view you can add your textField or button whatever you want as per your requirement.2) now when user starts dragging tableview (actual scrollview of table view) you can call scrollview's delegate methods according it to test it.
When you dragging/scrolling tableView down then you will get contentOffset.y will be less then 0, I have explain here in code.
This two steps are working fine for me, as i have implemented. Let me add images to verify it.
if you still have any queries you can ask me.
Swift 2.0:
I worked out Nirav's answer in swift Xcode 7.1. Have a look.
Adding scroll method using UIScrollViewDelegate.
Use
UISearchBar
instead ofUITextField
. And add it astableView's headerView
Eg: