Pull down to show view

三世轮回 提交于 2020-03-17 11:49:07

问题


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;
    }
}

回答1:


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.




回答2:


Swift 2.0:

I worked out Nirav's answer in swift Xcode 7.1. Have a look.

let footerView = UIView()
let scroll = UIScrollView()

override func viewDidLoad() {
    super.viewDidLoad()
    var searchBar:UISearchBar?
    searchBar = UISearchBar(frame: CGRectMake(0, 0, 320, 44))
    searchBar!.delegate = self
    searchBar!.tintColor = UIColor.orangeColor()
    footerView.addSubview(searchBar!)

    footerView.frame = CGRectMake(0, -50, 320, 50)
    footerView.backgroundColor =  UIColor.clearColor()
    self.listWrapTableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
    self.listWrapTableView.addSubview(footerView)
        }

Adding scroll method using UIScrollViewDelegate.

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
            if(scrollView.contentOffset.y < 0){
                print("greater than table height")
                UIView.beginAnimations(nil, context: nil)
                UIView.setAnimationDuration(0.2)
                self.listWrapTableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0)
                UIView.commitAnimations()
            }
            else
            {
                UIView.beginAnimations(nil, context: nil)
                UIView.setAnimationDuration(0.2)
                self.listWrapTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
                UIView.commitAnimations()
            }
        }



回答3:


Use UISearchBar instead of UITextField. And add it as tableView's headerView

Eg:

UISearchBar *mySearchBar = [[UISearchBar alloc] init];
myTableIvew.tableHeaderView = mySearchBar;


来源:https://stackoverflow.com/questions/21670202/pull-down-to-show-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!