Skip to content

Commit d7656c8

Browse files
committed
Also scroll to the top if already at the first view controller
1 parent 8399e7c commit d7656c8

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

trySwift/RootTabBar/RootTabBarController.swift

+29-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,36 @@ class RootTabBarController: UITabBarController, UITabBarControllerDelegate {
1616

1717
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
1818
if let splitViewController = viewController as? UISplitViewController {
19-
if let navigationController = splitViewController.viewControllers.last as? UINavigationController,
20-
navigationController.viewControllers.count > 1 {
21-
navigationController.popViewController(animated: true)
19+
if let navigationController = splitViewController.viewControllers.last as? UINavigationController {
20+
if navigationController.viewControllers.count > 1 {
21+
navigationController.popViewController(animated: true)
22+
} else {
23+
navigationController.viewControllers.first?.scrollToTop()
24+
}
2225
}
2326
}
2427
}
2528
}
29+
30+
private extension UIViewController {
31+
func scrollToTop() {
32+
let scrollView = view.findScrollSubview()
33+
scrollView?.setContentOffset(.zero, animated: true)
34+
}
35+
}
36+
37+
private extension UIView {
38+
func findScrollSubview() -> UIScrollView? {
39+
if let scrollView = self as? UIScrollView,
40+
scrollView.contentSize.width < scrollView.contentSize.height {
41+
return scrollView
42+
}
43+
for subview in self.subviews {
44+
if let scrollView = subview.findScrollSubview(),
45+
scrollView.contentSize.width < scrollView.contentSize.height {
46+
return scrollView
47+
}
48+
}
49+
return nil
50+
}
51+
}

0 commit comments

Comments
 (0)