Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pull down to show view

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;
    }
}
like image 299
Valentin Shamardin Avatar asked Feb 10 '14 06:02

Valentin Shamardin


1 Answers

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.

enter image description here

enter image description here

if you still have any queries you can ask me.

like image 79
Nirav Jain Avatar answered Sep 23 '22 13:09

Nirav Jain