下拉以显示视图 [英] Pull down to show view

查看:240
本文介绍了下拉以显示视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有UITableView.我想在tableView上方添加一个UITextField,可以通过向下拖动tableView进行访问.我想通过向上拉tableView隐藏我的textField.我怎样才能做到这一点? 这是我尝试过的:

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)添加one view to negative position of tableView.在此视图中,您可以根据需要添加textField或按钮.

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)现在,当用户开始拖动表格视图(表格视图的实际滚动视图)时,您可以根据其调用滚动视图的委托方法进行测试.

2) now when user starts dragging tableview (actual scrollview of table view) you can call scrollview's delegate methods according it to test it.

当您向下拖动/滚动tableView时,您将得到contentOffset.y小于0,我在代码中已对此进行了解释.

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.

这篇关于下拉以显示视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆