Tusker/Tusker/Screens/Utilities/EnhancedTableViewController...

39 lines
1.3 KiB
Swift

//
// EnhancedTableViewController.swift
// Tusker
//
// Created by Shadowfacts on 11/10/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
class EnhancedTableViewController: UITableViewController {
var prevScrollToTopOffset: CGPoint? = nil
private var topOffset: CGPoint {
// when scrolled to top, the content offset is negative the height of the UI above the scroll view (i.e. the nav and status bars)
let windowScene = UIApplication.shared.keyWindow!.windowScene!
let barOffset = -1 * (navigationController!.navigationBar.frame.height + windowScene.statusBarManager!.statusBarFrame.height)
// add one so it's not technically all the way at the top, and scrollViewWShouldScrollToTop is still called to trigger undo
return CGPoint(x: 0, y: barOffset + 1)
}
override func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
if let offset = prevScrollToTopOffset {
tableView.setContentOffset(offset, animated: true)
prevScrollToTopOffset = nil
} else {
prevScrollToTopOffset = tableView.contentOffset
tableView.setContentOffset(topOffset, animated: true)
}
return false
}
override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
prevScrollToTopOffset = nil
}
}