Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable a containerView to be narrower than the containing view controller #625

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Sources/PagerTabStripViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -199,7 +202,12 @@ 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
if allowNarrowerContainerView {
inset = 0
}

return (CGFloat(index) * containerView.bounds.width) + inset
}

open func offsetForChild(viewController: UIViewController) throws -> CGFloat {
Expand Down Expand Up @@ -241,13 +249,14 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
for (index, childController) in pagerViewControllers.enumerated() {
let pageOffsetForChild = self.pageOffsetForChild(at: index)
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: 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)
addChild(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(toParent: self)
Expand Down