From 515cf088f6f56b1bfd2b8f802d231ca7b523d2e9 Mon Sep 17 00:00:00 2001 From: Christopher Prince Date: Wed, 26 Sep 2018 08:25:33 -0600 Subject: [PATCH 1/3] Enable container view to be narrower (less wide) than self.view. --- Sources/PagerTabStripViewController.swift | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Sources/PagerTabStripViewController.swift b/Sources/PagerTabStripViewController.swift index 41f47c41..79f845f3 100644 --- a/Sources/PagerTabStripViewController.swift +++ b/Sources/PagerTabStripViewController.swift @@ -52,6 +52,9 @@ public protocol PagerTabStripDataSource: class { open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate { @IBOutlet weak public var containerView: UIScrollView! + + // Set this to true to enable the containerView to be narrower (less wide) than self.view; leaving this as `false` gives the original behavior of this class. + open var allowNarrowerContainerView: Bool = false open weak var delegate: PagerTabStripDelegate? open weak var datasource: PagerTabStripDataSource? @@ -199,7 +202,11 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate { } open func offsetForChild(at index: Int) -> CGFloat { - return (CGFloat(index) * containerView.bounds.width) + ((containerView.bounds.width - view.bounds.width) * 0.5) + var inset = (containerView.bounds.width - view.bounds.width) * 0.5 + inset = 0 + } + + return (CGFloat(index) * containerView.bounds.width) + inset } open func offsetForChild(viewController: UIViewController) throws -> CGFloat { @@ -241,13 +248,14 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate { for (index, childController) in pagerViewControllers.enumerated() { let pageOffsetForChild = self.pageOffsetForChild(at: index) if fabs(containerView.contentOffset.x - pageOffsetForChild) < containerView.bounds.width { + // 9/26/18; Prior to this, in both of these blocks of code below, `containerView.bounds.width` was `view.bounds.width`. But why would you want to use the containerView height but not its width? Seems like a bug. if childController.parent != nil { - childController.view.frame = CGRect(x: offsetForChild(at: index), y: 0, width: view.bounds.width, height: containerView.bounds.height) + childController.view.frame = CGRect(x: offsetForChild(at: index), y: 0, width: containerView.bounds.width, height: containerView.bounds.height) childController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth] } else { childController.beginAppearanceTransition(true, animated: false) addChildViewController(childController) - childController.view.frame = CGRect(x: offsetForChild(at: index), y: 0, width: view.bounds.width, height: containerView.bounds.height) + childController.view.frame = CGRect(x: offsetForChild(at: index), y: 0, width: containerView.bounds.width, height: containerView.bounds.height) childController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth] containerView.addSubview(childController.view) childController.didMove(toParentViewController: self) From 76c7d75f713d17a2d8c65cfaf9d8fa481e98bb4b Mon Sep 17 00:00:00 2001 From: Christopher Prince Date: Wed, 26 Sep 2018 08:25:48 -0600 Subject: [PATCH 2/3] See prior commit message. --- Sources/PagerTabStripViewController.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/PagerTabStripViewController.swift b/Sources/PagerTabStripViewController.swift index 79f845f3..8e85d7fd 100644 --- a/Sources/PagerTabStripViewController.swift +++ b/Sources/PagerTabStripViewController.swift @@ -203,6 +203,7 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate { open func offsetForChild(at index: Int) -> CGFloat { var inset = (containerView.bounds.width - view.bounds.width) * 0.5 + if allowNarrowerContainerView { inset = 0 } From 260f02bd9e583c4c5c1fb2435000bc3ddf536b38 Mon Sep 17 00:00:00 2001 From: Christopher Prince Date: Thu, 29 Nov 2018 17:05:04 -0700 Subject: [PATCH 3/3] Fix build warning. --- Sources/PagerTabStripViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/PagerTabStripViewController.swift b/Sources/PagerTabStripViewController.swift index 6ceac730..88557595 100644 --- a/Sources/PagerTabStripViewController.swift +++ b/Sources/PagerTabStripViewController.swift @@ -248,7 +248,7 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate { for (index, childController) in pagerViewControllers.enumerated() { let pageOffsetForChild = self.pageOffsetForChild(at: index) - if fabs(containerView.contentOffset.x - pageOffsetForChild) < containerView.bounds.width { + if abs(containerView.contentOffset.x - pageOffsetForChild) < containerView.bounds.width { // 9/26/18; Prior to this, in both of these blocks of code below, `containerView.bounds.width` was `view.bounds.width`. But why would you want to use the containerView height but not its width? Seems like a bug. if childController.parent != nil { childController.view.frame = CGRect(x: offsetForChild(at: index), y: 0, width: containerView.bounds.width, height: containerView.bounds.height)