From f7fa3b4990ae2518de0908f771abeb36695e83cf Mon Sep 17 00:00:00 2001 From: Michal Klein Date: Wed, 6 Feb 2019 14:32:55 +0100 Subject: [PATCH 1/3] Select currentIndex pre-viewDidLoad --- Sources/PagerTabStripViewController.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/PagerTabStripViewController.swift b/Sources/PagerTabStripViewController.swift index d7e5551a..ddb79334 100644 --- a/Sources/PagerTabStripViewController.swift +++ b/Sources/PagerTabStripViewController.swift @@ -149,7 +149,11 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate { } open func moveToViewController(at index: Int, animated: Bool = true) { - guard isViewLoaded && view.window != nil && currentIndex != index else { + guard isViewLoaded && !viewControllers.isEmpty && view.window != nil else { + // currentIndex and preCurrentIndex are compared in viewDidAppear + // If they differ, animated moveToViewController is called. + // For pre-viewDidLoad setup, we need to make sure the indices are same. + currentIndex = index preCurrentIndex = index return } From 2c27c7b018715600881e0f992dd147a009e63a24 Mon Sep 17 00:00:00 2001 From: Michal Klein Date: Wed, 6 Feb 2019 14:35:41 +0100 Subject: [PATCH 2/3] Fix updateContent When lastSize.width != containerView.bounds.size.width, the updateContent was called twice and in the wrong order, sometimes causing unwanted behavior. --- Sources/PagerTabStripViewController.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/PagerTabStripViewController.swift b/Sources/PagerTabStripViewController.swift index ddb79334..12028545 100644 --- a/Sources/PagerTabStripViewController.swift +++ b/Sources/PagerTabStripViewController.swift @@ -233,9 +233,11 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate { } open func updateContent() { - if lastSize.width != containerView.bounds.size.width { + guard lastSize.width == containerView.bounds.size.width else { lastSize = containerView.bounds.size + // Settings contentOffset calls scrollViewDidScroll which calls updateContent again. containerView.contentOffset = CGPoint(x: pageOffsetForChild(at: currentIndex), y: 0) + return } lastSize = containerView.bounds.size From 41c440d810d020abba7569d9fbf2162247625755 Mon Sep 17 00:00:00 2001 From: Michal Klein Date: Wed, 6 Feb 2019 14:36:59 +0100 Subject: [PATCH 3/3] Force updateContent call on first viewWillAppear This fixes the initial contentOffset setup --- Sources/PagerTabStripViewController.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sources/PagerTabStripViewController.swift b/Sources/PagerTabStripViewController.swift index 12028545..50090202 100644 --- a/Sources/PagerTabStripViewController.swift +++ b/Sources/PagerTabStripViewController.swift @@ -113,6 +113,10 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate { open override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) + if !didCallViewWillAppearOnce { + updateContent() + didCallViewWillAppearOnce = true + } isViewAppearing = true children.forEach { $0.beginAppearanceTransition(true, animated: animated) } } @@ -398,5 +402,6 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate { private var lastSize = CGSize(width: 0, height: 0) internal var isViewRotating = false internal var isViewAppearing = false + private var didCallViewWillAppearOnce = false }