From 9c8437fe6c1dc482fad0419716f611a47d952fb2 Mon Sep 17 00:00:00 2001 From: Krishna Kishore Bandaru Date: Wed, 13 Mar 2024 22:28:52 +0530 Subject: [PATCH] Fixed layout issues --- VDS.xcodeproj/project.pbxproj | 8 +-- VDS/Components/Pagination/Pagination.swift | 62 ++++++++++++------- ...ew.swift => PaginationContainerView.swift} | 25 +------- .../Pagination/PaginationFlowLayout.swift | 5 +- 4 files changed, 48 insertions(+), 52 deletions(-) rename VDS/Components/Pagination/{PaginationCollectionView.swift => PaginationContainerView.swift} (52%) diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index 38a79f1e..f57edb0f 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -16,7 +16,7 @@ 5F21D7BF28DCEB3D003E7CD6 /* Useable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F21D7BE28DCEB3D003E7CD6 /* Useable.swift */; }; 5FC35BE328D51405004EBEAC /* Button.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FC35BE228D51405004EBEAC /* Button.swift */; }; 7115BD3C2B84C0C200E0A610 /* TileContainerChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */; }; - 71ACE89C2BA0451200FB6ADC /* PaginationCollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71ACE89B2BA0451200FB6ADC /* PaginationCollectionView.swift */; }; + 71ACE89C2BA0451200FB6ADC /* PaginationContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71ACE89B2BA0451200FB6ADC /* PaginationContainerView.swift */; }; 71B23C2D2B91FA690027F7D9 /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71B23C2C2B91FA690027F7D9 /* Pagination.swift */; }; 71B5FCBB2B95A0CA00269BCC /* PaginationChangeLog.txt in Resources */ = {isa = PBXBuildFile; fileRef = 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */; }; 71BFA70A2B7F70E6000DCE33 /* DropShadowable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71BFA7092B7F70E6000DCE33 /* DropShadowable.swift */; }; @@ -193,7 +193,7 @@ 5F21D7BE28DCEB3D003E7CD6 /* Useable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Useable.swift; sourceTree = ""; }; 5FC35BE228D51405004EBEAC /* Button.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = ""; }; 7115BD3B2B84C0C200E0A610 /* TileContainerChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = TileContainerChangeLog.txt; sourceTree = ""; }; - 71ACE89B2BA0451200FB6ADC /* PaginationCollectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationCollectionView.swift; sourceTree = ""; }; + 71ACE89B2BA0451200FB6ADC /* PaginationContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationContainerView.swift; sourceTree = ""; }; 71B23C2C2B91FA690027F7D9 /* Pagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pagination.swift; sourceTree = ""; }; 71B5FCBA2B95A0CA00269BCC /* PaginationChangeLog.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = PaginationChangeLog.txt; sourceTree = ""; }; 71BFA7092B7F70E6000DCE33 /* DropShadowable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropShadowable.swift; sourceTree = ""; }; @@ -406,7 +406,7 @@ isa = PBXGroup; children = ( 71B23C2C2B91FA690027F7D9 /* Pagination.swift */, - 71ACE89B2BA0451200FB6ADC /* PaginationCollectionView.swift */, + 71ACE89B2BA0451200FB6ADC /* PaginationContainerView.swift */, 71FC86D92B96F44C00700965 /* PaginationButton.swift */, 71FC86DB2B96F4C800700965 /* PaginationCellItem.swift */, 71FC86E32B9841AC00700965 /* PaginationFlowLayout.swift */, @@ -1059,7 +1059,7 @@ EAC925842911C63100091998 /* Colorable.swift in Sources */, EAB5FEF5292D371F00998C17 /* ButtonBase.swift in Sources */, EA978EC5291D6AFE00ACC883 /* AnyLabelAttribute.swift in Sources */, - 71ACE89C2BA0451200FB6ADC /* PaginationCollectionView.swift in Sources */, + 71ACE89C2BA0451200FB6ADC /* PaginationContainerView.swift in Sources */, EAC71A1F2A2E173D00E47A9F /* RadioButton.swift in Sources */, EA33622C2891E73B0071C351 /* FontProtocol.swift in Sources */, EA596ABD2A16B4EC00300C4B /* Tab.swift in Sources */, diff --git a/VDS/Components/Pagination/Pagination.swift b/VDS/Components/Pagination/Pagination.swift index 754935b7..7728d6dc 100644 --- a/VDS/Components/Pagination/Pagination.swift +++ b/VDS/Components/Pagination/Pagination.swift @@ -16,21 +16,36 @@ open class Pagination: View { //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- + ///Maximum component width + private let maxWidth: CGFloat = 288.0 ///Collectionview width anchor private var collectionViewWidthAnchor: NSLayoutConstraint? + ///Collectionview container Center X constraint + private var collectionContainerViewCenterXConstraint: NSLayoutConstraint? ///Selected page index private var _selectedPageIndex: Int = 0 ///Custom flow layout defined for the Pagination - private var flowLayout: PaginationFlowLayout { - guard let flowLayout = collectionContainerView.collectionView.collectionViewLayout as? PaginationFlowLayout else { fatalError("Flow layout should be PaginationFlowLayout class") } - return flowLayout - } + private let flowLayout = PaginationFlowLayout() ///A root view for the pagination - private let containerView: View = View().with { + public let containerView: View = View().with { $0.translatesAutoresizingMaskIntoConstraints = false } - ///Container view to hold collectionview to render pagination indexes - private let collectionContainerView = PaginationCollectionView() + ///Collectionview to render pagination indexes + private lazy var collectionView: UICollectionView = { + let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) + collectionView.isScrollEnabled = false + collectionView.translatesAutoresizingMaskIntoConstraints = false + collectionView.showsHorizontalScrollIndicator = false + collectionView.showsVerticalScrollIndicator = false + collectionView.isAccessibilityElement = true + collectionView.register(PaginationCellItem.self, forCellWithReuseIdentifier: PaginationCellItem.identifier) + collectionView.backgroundColor = .clear + collectionView.delegate = self + collectionView.dataSource = self + return collectionView + }() + ///Container view to hold collectionview to render pagination indexes and to handler accessibility. + private let collectionContainerView = PaginationContainerView() //-------------------------------------------------- // MARK: - Public Properties @@ -84,26 +99,27 @@ open class Pagination: View { containerView.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor).activate() trailingAnchor.constraint(greaterThanOrEqualTo: containerView.trailingAnchor).activate() containerView.centerXAnchor.constraint(equalTo: centerXAnchor).activate() - containerView.widthAnchor.constraint(equalToConstant: 288).activate() + containerView.widthAnchor.constraint(equalToConstant: maxWidth).activate() containerView.heightAnchor.constraint(equalToConstant: 44).activate() containerView.addSubview(previousButton) containerView.addSubview(collectionContainerView) containerView.addSubview(nextButton) - + collectionContainerView.addSubview(collectionView) previousButton .pinTop() .pinBottom() .pinLeading() previousButton.trailingAnchor.constraint(greaterThanOrEqualTo: collectionContainerView.leadingAnchor).activate() - collectionContainerView.heightAnchor.constraint(equalToConstant: VDSLayout.Spacing.space4X.value).activate() - collectionContainerView.centerYAnchor.constraint(equalTo: centerYAnchor).activate() - collectionContainerView.centerXAnchor.constraint(equalTo: centerXAnchor).activate() collectionContainerView.trailingAnchor.constraint(greaterThanOrEqualTo: nextButton.leadingAnchor).activate() - collectionViewWidthAnchor = collectionContainerView.widthAnchor.constraint(equalToConstant: 92) + collectionContainerView + .pinTop() + .pinBottom() + collectionView.heightAnchor.constraint(equalToConstant: VDSLayout.Spacing.space4X.value).activate() + collectionView.centerYAnchor.constraint(equalTo: centerYAnchor).activate() + collectionView.centerXAnchor.constraint(equalTo: collectionContainerView.centerXAnchor).activate() + collectionViewWidthAnchor = collectionView.widthAnchor.constraint(equalToConstant: 92) collectionViewWidthAnchor?.activate() - collectionContainerView.collectionView.delegate = self - collectionContainerView.collectionView.dataSource = self nextButton .pinTop() @@ -142,7 +158,7 @@ open class Pagination: View { super.updateView() nextButton.surface = surface previousButton.surface = surface - collectionContainerView.collectionView.reloadData() + collectionView.reloadData() } //-------------------------------------------------- @@ -163,10 +179,10 @@ open class Pagination: View { private func updateSelection() { guard _selectedPageIndex < total else { return } //Need to make selected page as second element so scrolling previous index of the selected page to left - collectionContainerView.collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) + collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) previousButton.isHidden = _selectedPageIndex == 0 nextButton.isHidden = _selectedPageIndex == total - 1 - collectionContainerView.collectionView.reloadData() + collectionView.reloadData() verifyIfMaxDigitChanged() } @@ -175,17 +191,17 @@ open class Pagination: View { let upperLimitPage = _selectedPageIndex + flowLayout.maxNumberOfColumns let upperLimitDigits = upperLimitPage.digitCount //future value digits switch (flowLayout.numberOfColumns, upperLimitDigits) { - case (_, 3), (_, 4): - flowLayout.numberOfColumns = 3 - default: + case (_, 1), (_, 2): flowLayout.numberOfColumns = 4 + default: + flowLayout.numberOfColumns = 3 } if upperLimitDigits != flowLayout.upperLimitDigits { flowLayout.upperLimitDigits = upperLimitDigits flowLayout.invalidateLayout() - collectionContainerView.collectionView.reloadData() + collectionView.reloadData() //Need to make selected page as second element so scrolling previous index of the selected page to left - collectionContainerView.collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) + collectionView.scrollToItem(at: IndexPath(row: max(_selectedPageIndex - 1, 0), section: 0), at: .left, animated: false) } } } diff --git a/VDS/Components/Pagination/PaginationCollectionView.swift b/VDS/Components/Pagination/PaginationContainerView.swift similarity index 52% rename from VDS/Components/Pagination/PaginationCollectionView.swift rename to VDS/Components/Pagination/PaginationContainerView.swift index a41c65ef..b79a2fe2 100644 --- a/VDS/Components/Pagination/PaginationCollectionView.swift +++ b/VDS/Components/Pagination/PaginationContainerView.swift @@ -1,5 +1,5 @@ // -// PaginationCollectionView.swift +// PaginationContainerView.swift // VDS // // Created by Bandaru, Krishna Kishore on 12/03/24. @@ -8,7 +8,7 @@ import UIKit ///PaginationCollectionView is a container view that holds collectionview for displaying page indexes -final class PaginationCollectionView: View { +final class PaginationContainerView: View { //-------------------------------------------------- // MARK: - Internal Properties @@ -17,24 +17,6 @@ final class PaginationCollectionView: View { var onAccessibilityIncrement: (() -> Void)? ///Notifies when accessibility decrement is happend when user swipes down var onAccessibilityDecrement: (() -> Void)? - ///Collectionview to render pagination indexes - lazy var collectionView: UICollectionView = { - let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) - collectionView.isScrollEnabled = false - collectionView.translatesAutoresizingMaskIntoConstraints = false - collectionView.showsHorizontalScrollIndicator = false - collectionView.showsVerticalScrollIndicator = false - collectionView.isAccessibilityElement = true - collectionView.register(PaginationCellItem.self, forCellWithReuseIdentifier: PaginationCellItem.identifier) - collectionView.backgroundColor = .clear - return collectionView - }() - - //-------------------------------------------------- - // MARK: - Private Properties - //-------------------------------------------------- - ///Custom flow layout defined for the Pagination - private let flowLayout = PaginationFlowLayout() //-------------------------------------------------- // MARK: - Overrides @@ -58,9 +40,6 @@ final class PaginationCollectionView: View { /// Executed on initialization for this View. override func setup() { super.setup() - addSubview(collectionView) - collectionView.pinToSuperView() isAccessibilityElement = true - accessibilityElements = [collectionView] } } diff --git a/VDS/Components/Pagination/PaginationFlowLayout.swift b/VDS/Components/Pagination/PaginationFlowLayout.swift index 542e4eff..c6bd02c3 100644 --- a/VDS/Components/Pagination/PaginationFlowLayout.swift +++ b/VDS/Components/Pagination/PaginationFlowLayout.swift @@ -18,8 +18,9 @@ final class PaginationFlowLayout : UICollectionViewLayout { ///Pre-defined sizes of the pagination cell based on number of digits. private var upperLimitSize: CGSize { switch upperLimitDigits { - case 3, 4: .init(width: 34, height: 16) - default: .init(width: 20, height: 16) + case 1, 2: .init(width: 20, height: 16) + case 3: .init(width: 28, height: 16) + default: .init(width: 34, height: 16) } } ///Property to store the defined layout attributes.