iOS 11 scroll to top when using large titles doesn't work properly

被刻印的时光 ゝ 提交于 2019-12-05 12:45:33

Okay so I found why the problem occurs, but not how to fix it in that exact scenario.

If you're using large titles and a UITableViewController with the navigation bar translucency set to off the problem will occur. When you turn translucent back on the problem goes away.

If you're using a TableView in a normal UIViewController the problem always occurs.

Edit

Turns out setting "extendedLayoutIncludesOpaqueBars = true" fixes the problem if you're using a translucent navigation bar!

Similar question: UIRefreshControl() in iOS 11 Glitchy effect

After many hours of tests i found a solution that works with UIViewController.

In UIViewController with UITableView, you should:

  • in viewDidLoad set extendedLayoutIncludesOpaqueBars = true
  • in storyboard pin tableView top constraint to superview top with constant = 0 (tableView will be under navigationbar and statusbar when navigationbar is translucent)

After that if you tap on statusbar, tableview stops in the right place.

I've fix it with maually setting the contentOffset to 0 after scroll to top.

func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
    return true
}

func scrollViewDidScrollToTop(_ scrollView: UIScrollView) {
    scrollView.contentOffset.y = 0.0
}

In your ViewController declare a var didBeginScrollToTop of Bool type. Assign the controller as scrollView's delegate. Then

func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
    didBeginScrollToTop = true
    return true
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    guard didBeginScrollToTop, scrollToTopGestureTargetView?.contentOffset.y ?? 0 < -25 else { return }

    scrollToTopGestureTargetView?.setContentOffset(.zero, animated: true)
}

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    didBeginScrollToTop = false
}

What this does is adjusting your content offset when bouncing. The flag is there to catch the scrollToTop gesture and is reset on scroll. You can also set it off after setting the .zero offset. If you have implementation with child VCs, don't forget to call super when overriding those 3 delegate methods. Now my first approach was to track when the content offSet is < 0, but on some devices it did not expand the navigation item and that magic -25 seemed to work on all simulators. You can combine this approach with an implementation that returns the size of the nav bar and replace it, but as far as I am aware there wasn't an easy way to get the nav bar frame/bounds so easily.

Note you may have to handle also this: Strange velocity prefers large title

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