【问题标题】:Pull down to show view下拉显示视图
【发布时间】:2014-02-10 06:11:14
【问题描述】:

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

【问题讨论】:

  • 什么对你有用?是否添加了 textField ?
  • @SamkitJain,是的,它已添加。但我无法正确显示和隐藏 textField
  • 有什么问题?它会在一段时间内加载吗?
  • 文本字段有固定位置吗?

标签: ios objective-c uitableview uiscrollview


【解决方案1】:

我在我的应用程序中完成了此类功能。我所做的只是按照步骤操作。

1) 添加one view to negative position of tableView。在此视图中,您可以根据需要添加任何您想要的文本字段或按钮。

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(table view的实际滚动视图)时,您可以根据它调用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];   
    }
}

这两个步骤对我来说很好,因为我已经实现了。让我添加图像来验证它。

如果你还有什么疑问可以问我。

【讨论】:

  • 谢谢,这就是我想要的!
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多