From 7dcbfa0988e28a45a8e7f950a1f1b865d2974b27 Mon Sep 17 00:00:00 2001 From: Nishan Date: Sun, 14 Apr 2019 19:15:08 +0545 Subject: [PATCH] Fix lint issue for 2.0.0 release --- ViewPager-Swift/Core/ViewPagerTabView.swift | 49 +++++++++++---------- ViewPager-Swift/ItemViewController.swift | 4 +- ViewPager-Swift/ViewPager.swift | 25 ++++------- 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/ViewPager-Swift/Core/ViewPagerTabView.swift b/ViewPager-Swift/Core/ViewPagerTabView.swift index 4c01d52..a5312bc 100644 --- a/ViewPager-Swift/Core/ViewPagerTabView.swift +++ b/ViewPager-Swift/Core/ViewPagerTabView.swift @@ -60,27 +60,20 @@ public final class ViewPagerTabView: UIView { */ fileprivate func setupBasicTab(options:ViewPagerOptions, tab:ViewPagerTab) { + buildTitleLabel(withOptions: options, text: tab.title) + + setupForAutolayout(view: titleLabel) + titleLabel?.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true + titleLabel?.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true + titleLabel?.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true + titleLabel?.topAnchor.constraint(equalTo: self.topAnchor).isActive = true let distribution = options.distribution - switch distribution { - case .segmented: - - buildTitleLabel(withOptions: options, text: tab.title) - - titleLabel?.setupForAutolayout(inView: self) - titleLabel?.pinSides(inView: self) - - case .equal, .normal: - - buildTitleLabel(withOptions: options, text: tab.title) - - let labelWidth = titleLabel!.intrinsicContentSize.width + options.tabViewPaddingLeft + options.tabViewPaddingRight - self.width = labelWidth - - titleLabel?.setupForAutolayout(inView: self) - titleLabel?.pinSides(inView: self) - } + guard distribution == .equal || distribution == .normal else { return } + + let labelWidth = titleLabel!.intrinsicContentSize.width + options.tabViewPaddingLeft + options.tabViewPaddingRight + self.width = labelWidth } /** @@ -109,13 +102,13 @@ public final class ViewPagerTabView: UIView { buildImageView(withOptions: options, image: tab.image) buildTitleLabel(withOptions: options, text: tab.title) - imageView?.setupForAutolayout(inView: self) + setupForAutolayout(view: imageView) imageView?.heightAnchor.constraint(equalToConstant: imageSize.height).isActive = true imageView?.widthAnchor.constraint(equalToConstant: imageSize.width).isActive = true imageView?.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true imageView?.topAnchor.constraint(equalTo: self.topAnchor, constant: options.tabViewImageMarginTop).isActive = true - titleLabel?.setupForAutolayout(inView: self) + setupForAutolayout(view: titleLabel) titleLabel?.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true titleLabel?.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true titleLabel?.topAnchor.constraint(equalTo: imageView!.bottomAnchor, constant: options.tabViewImageMarginBottom).isActive = true @@ -125,7 +118,7 @@ public final class ViewPagerTabView: UIView { buildImageView(withOptions: options, image: tab.image) - imageView?.setupForAutolayout(inView: self) + setupForAutolayout(view: imageView) imageView?.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true imageView?.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true imageView?.widthAnchor.constraint(equalToConstant: imageSize.width).isActive = true @@ -140,13 +133,13 @@ public final class ViewPagerTabView: UIView { buildImageView(withOptions: options, image: tab.image) buildTitleLabel(withOptions: options, text: tab.title) - imageView?.setupForAutolayout(inView: self) + setupForAutolayout(view: imageView) imageView?.heightAnchor.constraint(equalToConstant: imageSize.height).isActive = true imageView?.widthAnchor.constraint(equalToConstant: imageSize.width).isActive = true imageView?.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true imageView?.topAnchor.constraint(equalTo: self.topAnchor, constant: options.tabViewImageMarginTop).isActive = true - titleLabel?.setupForAutolayout(inView: self) + setupForAutolayout(view: titleLabel) titleLabel?.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true titleLabel?.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true titleLabel?.topAnchor.constraint(equalTo: imageView!.bottomAnchor, constant: options.tabViewImageMarginBottom).isActive = true @@ -163,7 +156,7 @@ public final class ViewPagerTabView: UIView { // Creating imageview buildImageView(withOptions: options, image: tab.image) - imageView?.setupForAutolayout(inView: self) + setupForAutolayout(view: imageView) imageView?.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true imageView?.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true imageView?.widthAnchor.constraint(equalToConstant: imageSize.width).isActive = true @@ -209,4 +202,12 @@ public final class ViewPagerTabView: UIView { self.backgroundColor = options.tabViewBackgroundDefaultColor self.titleLabel?.textColor = options.tabViewTextDefaultColor } + + internal func setupForAutolayout(view: UIView?) { + + guard let v = view else { return } + + v.translatesAutoresizingMaskIntoConstraints = false + self.addSubview(v) + } } diff --git a/ViewPager-Swift/ItemViewController.swift b/ViewPager-Swift/ItemViewController.swift index eeb99df..7bb37cf 100644 --- a/ViewPager-Swift/ItemViewController.swift +++ b/ViewPager-Swift/ItemViewController.swift @@ -24,7 +24,9 @@ class ItemViewController: UIViewController { super.viewDidLoad() let itemLabel = UILabel() - itemLabel.setupForAutolayout(inView: self.view) + itemLabel.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(itemLabel) + itemLabel.textAlignment = .center itemLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true itemLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true diff --git a/ViewPager-Swift/ViewPager.swift b/ViewPager-Swift/ViewPager.swift index a40eff9..b235e6c 100644 --- a/ViewPager-Swift/ViewPager.swift +++ b/ViewPager-Swift/ViewPager.swift @@ -132,7 +132,7 @@ public class ViewPager: NSObject { let pageController = UIPageViewController(transitionStyle: options.viewPagerTransitionStyle, navigationOrientation: .horizontal, options: nil) self.controller?.addChild(pageController) - pageController.view.setupForAutolayout(inView: view) + setupForAutolayout(view: pageController.view, inView: view) pageController.didMove(toParent: controller) self.pageController = pageController @@ -175,7 +175,7 @@ public class ViewPager: NSObject { if options.isTabIndicatorAvailable { - tabIndicator.setupForAutolayout(inView: self.tabContainer) + setupForAutolayout(view: tabIndicator, inView: tabContainer) tabIndicator.backgroundColor = options.tabIndicatorViewBackgroundColor tabIndicator.heightAnchor.constraint(equalToConstant: options.tabIndicatorViewHeight).isActive = true tabIndicator.bottomAnchor.constraint(equalTo: tabContainer.bottomAnchor).isActive = true @@ -214,7 +214,7 @@ public class ViewPager: NSObject { for (index, eachTab) in tabsList.enumerated() { let tabView = ViewPagerTabView() - tabView.setupForAutolayout(inView: tabContainer) + setupForAutolayout(view: tabView, inView: tabContainer) tabView.backgroundColor = options.tabViewBackgroundDefaultColor tabView.setup(tab: eachTab, options: options) @@ -264,7 +264,7 @@ public class ViewPager: NSObject { for (index, eachTab) in self.tabsList.enumerated() { let tabView = ViewPagerTabView() - tabView.setupForAutolayout(inView: tabContainer) + setupForAutolayout(view: tabView, inView: tabContainer) tabView.backgroundColor = options.tabViewBackgroundDefaultColor if let previousTab = lastTab { @@ -446,20 +446,13 @@ extension ViewPager: UIPageViewControllerDelegate { let pageIndex = pendingViewControllers.first?.view.tag delegate?.willMoveToControllerAtIndex(index: pageIndex!) } -} - -extension UIView { - func setupForAutolayout(inView v: UIView) { - v.addSubview(self) - self.translatesAutoresizingMaskIntoConstraints = false - } - func pinSides(inView v: UIView) { + internal func setupForAutolayout(view: UIView?, inView parentView: UIView) { + + guard let v = view else { return } - self.leadingAnchor.constraint(equalTo: v.leadingAnchor).isActive = true - self.trailingAnchor.constraint(equalTo: v.trailingAnchor).isActive = true - self.bottomAnchor.constraint(equalTo: v.bottomAnchor).isActive = true - self.topAnchor.constraint(equalTo: v.topAnchor).isActive = true + v.translatesAutoresizingMaskIntoConstraints = false + parentView.addSubview(v) } }