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

Fix issue 82 Gallery crashes if it has only one tab #83

Open
wants to merge 1 commit 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
74 changes: 41 additions & 33 deletions Sources/Utils/Pages/PagesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class PagesController: UIViewController {

lazy var scrollView: UIScrollView = self.makeScrollView()
lazy var scrollViewContentView: UIView = UIView()
lazy var pageIndicator: PageIndicator = self.makePageIndicator()

lazy var pageIndicator: PageIndicator? = self.makePageIndicator()
var selectedIndex: Int = 0
let once = Once()

Expand Down Expand Up @@ -74,43 +74,30 @@ class PagesController: UIViewController {

return scrollView
}

func makePageIndicator() -> PageIndicator {
let items = controllers.flatMap { $0.title }
let indicator = PageIndicator(items: items)
indicator.delegate = self

return indicator

func makePageIndicator() -> PageIndicator? {
if shouldUsePageIndicator() {
let items = controllers.flatMap { $0.title }
let indicator = PageIndicator(items: items)
indicator.delegate = self
return indicator
}
return nil
}

// MARK: - Setup

func setup() {
let usePageIndicator = controllers.count > 1
if usePageIndicator {
view.addSubview(pageIndicator)
Constraint.on(
pageIndicator.leftAnchor.constraint(equalTo: pageIndicator.superview!.leftAnchor),
pageIndicator.rightAnchor.constraint(equalTo: pageIndicator.superview!.rightAnchor),
pageIndicator.heightAnchor.constraint(equalToConstant: 40)
)

if #available(iOS 11, *) {
Constraint.on(
pageIndicator.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
)
} else {
Constraint.on(
pageIndicator.bottomAnchor.constraint(equalTo: pageIndicator.superview!.bottomAnchor)
)
}
}

view.addSubview(scrollView)
scrollView.addSubview(scrollViewContentView)


if let indicator = pageIndicator {
view.addSubview(indicator)
addConstraintForPageIndicator(indicator)
}

scrollView.g_pinUpward()
if usePageIndicator {
if shouldUsePageIndicator() {
scrollView.g_pin(on: .bottom, view: pageIndicator, on: .top)
} else {
scrollView.g_pinDownward()
Expand Down Expand Up @@ -153,7 +140,7 @@ class PagesController: UIViewController {

fileprivate func scrollToAndSelect(index: Int, animated: Bool) {
scrollTo(index: index, animated: animated)
pageIndicator.select(index: index, animated: animated)
pageIndicator?.select(index: index, animated: animated)
}

func updateAndNotify(_ index: Int) {
Expand All @@ -168,6 +155,26 @@ class PagesController: UIViewController {
controller.pageDidShow()
}
}

private func shouldUsePageIndicator() -> Bool {
return controllers.count > 1
}

private func addConstraintForPageIndicator(_ pageIndicator: PageIndicator) {

Constraint.on(
pageIndicator.leftAnchor.constraint(equalTo: pageIndicator.superview!.leftAnchor),
pageIndicator.rightAnchor.constraint(equalTo: pageIndicator.superview!.rightAnchor),
pageIndicator.heightAnchor.constraint(equalToConstant: 40)
)

if #available(iOS 11, *) {
Constraint.on(pageIndicator.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor))
} else {
Constraint.on(pageIndicator.bottomAnchor.constraint(equalTo: pageIndicator.superview!.bottomAnchor))
}
}

}

extension PagesController: PageIndicatorDelegate {
Expand All @@ -182,7 +189,8 @@ extension PagesController: UIScrollViewDelegate {

func scrollViewDidScroll(_ scrollView: UIScrollView) {
let index = Int(round(scrollView.contentOffset.x / scrollView.frame.size.width))
pageIndicator.select(index: index)
pageIndicator?.select(index: index)
updateAndNotify(index)
}
}