下拉以显示视图

时间:2014-02-10 06:11:15

标签: ios objective-c uitableview uiscrollview

我有UITableView。我想在tableView上面添加一个UITextField,可以通过拉下tableView来访问它。我想通过拉取tableView来隐藏我的textField。我怎样才能做到这一点? 这是我试过的:

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

3 个答案:

答案 0 :(得分:14)

我在我的应用程序中完成了这样的功能。我做了什么只是按照步骤。

1)添加one view to negative position of tableView。在此视图中,您可以根据需要随意添加textField或按钮。

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)现在当用户开始拖动tableview(表视图的实际滚动视图)时,你可以调用scrollview的委托方法来测试它。

当您向下拖动/滚动tableView时,您将获得contentOffset.y将小于0,我已在代码中解释。

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

我已经实施了这两个步骤对我来说很好。让我添加图片来验证它。

enter image description here

enter image description here

如果您还有任何疑问,可以问我。

答案 1 :(得分:1)

Swift 2.0:

我在swift Xcode 7.1中找到了Nirav的答案。看看。

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

使用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()
            }
        }

答案 2 :(得分:-1)

使用UISearchBar代替UITextField。 并将其添加为tableView's headerView

例如:

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