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.
UIView *viewForSearchBar = [[UIView alloc]initWithFrame:CGRectMake(0, -50, 320, 50)];
viewForSearchBar.backgroundColor = [UIColor clearColor];
[self._tableView addSubview:viewForSearchBar];
self._tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
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.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (decelerate) {
[txtSearch resignFirstResponder];
}
id<UILayoutSupport> topLayoutGuide = self.topLayoutGuide;
if(scrollView.contentOffset.y < 0)
{
UIView* hiddenHeader = ...; // this points to the hidden header view above
CGRect headerFrame = [hiddenHeader frame];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
self._tableView.contentInset = UIEdgeInsetsMake(headerFrame.size.height + [topLayoutGuide length], 0, 0, 0);
[UIView commitAnimations];
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
self._tableView.contentInset = UIEdgeInsetsMake([topLayoutGuide length], 0, 0, 0);
[UIView commitAnimations];
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With