как я могу сделать привязку tableHeaderView и отсоединить ее к скрытой / скрытой позиции, когда пользователь прокручивает вверх - PullRequest
0 голосов
/ 22 мая 2018

У меня есть tableView.tableHeaderView, который является UITableViewHeaderFooterView.

Я бы хотел, чтобы этот заголовок изначально не отображался при первом представлении tableViewController.Когда пользователь прокручивает вниз от верхней части tableView, я хотел бы представить заголовок.

Затем, когда пользователь слегка прокручивает вниз, я хочу, чтобы заголовок переместился в скрытое положение.Поведение точно такое же, как и в заголовке заархивированных чатов на странице WhatsApp, где перечислены все ваши чаты.

Есть ли способ добиться этого без сложного набора вызовов делегатов scrollview?

Я подумалв предыдущих версиях swift / xcode метод tableView.tableHeaderView перемещался вверх и вниз, но я заметил, что он этого больше не делает.

Я думаю, что единственным решением может быть переопределение scrollViewDidScroll.Это то, что я сделал, но когда снова появляется tableView.headerView, он делает это над первой ячейкой tableView.Не уверен, как заставить его появиться в правильном положении.

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if let myHeaderView = self.tableView.tableHeaderView as? MyTableViewHeaderFooterView {
        let height = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height + self.searchController.searchBar.frame.size.height
        if scrollView.contentOffset.y < -height {
            UIView.animate(withDuration: 0.1) {
                myHeaderView.frame.size.height = 44
            }
        }
    }
}

1 Ответ

0 голосов
/ 22 мая 2018

Это то, на чем я остановился, и похоже, что оно работает довольно хорошо во всех случаях:

var minimumTableViewInset = CGFloat(88) // this can be adjusted to 32 when the tableView is in landscape.  need observer for rotation to set that.

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(UINib(nibName: "MyTableViewHeaderFooterView", bundle: nil), forHeaderFooterViewReuseIdentifier: "MyTableViewHeaderFooterView")

    let listsHeader = tableView.dequeueReusableHeaderFooterView(withIdentifier: "MyTableViewHeaderFooterView") as! MyTableViewHeaderFooterView

    listsHeader.alpha = 0
    listsHeader.frame.size.height = 0
    self.tableView.tableHeaderView = listsHeader
}

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if let headerView = self.tableView.tableHeaderView as? MyTableViewHeaderFooterView {
        let height = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height + self.searchController.searchBar.frame.size.height
        if scrollView.contentOffset.y < -height {
            if headerView.frame.size.height != 44 {
                tableView.beginUpdates()
                headerView.frame.size.height = 44
                tableView.endUpdates()
                UIView.animate(withDuration: 0.5) {
                    self.tableView.tableHeaderView?.alpha = 1
                }
            }
        }
    }
}

override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if let headerView = self.tableView.tableHeaderView as? MyTableViewHeaderFooterView {
        if scrollView.contentOffset.y > -(minimumTableViewInset - CGFloat(10)) {
            if headerView.frame.size.height != 0 {
                tableView.beginUpdates()
                headerView.frame.size.height = 0
                tableView.endUpdates()
                UIView.animate(withDuration: 0.2) {
                    self.tableView.tableHeaderView?.alpha = 0
                }
                tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.bottom, animated: true)
            }
        }
    }
}


override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    if let headerView = self.tableView.tableHeaderView as? MyTableViewHeaderFooterView {
        if scrollView.contentOffset.y > -(minimumTableViewInset - CGFloat(10)) {
            if headerView.frame.size.height != 0 {
                tableView.beginUpdates()
                headerView.frame.size.height = 0
                tableView.endUpdates()
                UIView.animate(withDuration: 0.2) {
                    self.tableView.tableHeaderView?.alpha = 0
                }
                tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.bottom, animated: true)
            }
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...