From 48e089bfab8c9e20e9e0faf7ea64dc219a22dad8 Mon Sep 17 00:00:00 2001 From: Kizito Nwose Date: Thu, 19 Apr 2018 12:44:32 +0100 Subject: [PATCH 1/3] add `willShow` and `didShow` methods to CountryPickerViewDelegate --- CountryPickerView/CountryPickerView.swift | 12 ++++++--- .../CountryPickerViewController.swift | 6 ++--- CountryPickerView/Delegate+DataSource.swift | 25 +++++++++++++++++++ CountryPickerView/Extensions.swift | 12 +++++++++ 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/CountryPickerView/CountryPickerView.swift b/CountryPickerView/CountryPickerView.swift index 18351a9..bf2f4ed 100644 --- a/CountryPickerView/CountryPickerView.swift +++ b/CountryPickerView/CountryPickerView.swift @@ -118,10 +118,16 @@ public class CountryPickerView: NibView { let countryVc = CountryPickerViewController(style: .grouped) countryVc.countryPickerView = self if let viewController = viewController as? UINavigationController { - viewController.pushViewController(countryVc, animated: true) + delegate?.countryPickerView(self, willShow: countryVc) + viewController.pushViewController(countryVc, animated: true) { + self.delegate?.countryPickerView(self, didShow: countryVc) + } } else { - viewController.present(UINavigationController(rootViewController: countryVc), - animated: true) + let navigationVC = UINavigationController(rootViewController: countryVc) + delegate?.countryPickerView(self, willShow: countryVc) + viewController.present(navigationVC, animated: true) { + self.delegate?.countryPickerView(self, didShow: countryVc) + } } } diff --git a/CountryPickerView/CountryPickerViewController.swift b/CountryPickerView/CountryPickerViewController.swift index bbb5326..fc69545 100644 --- a/CountryPickerView/CountryPickerViewController.swift +++ b/CountryPickerView/CountryPickerViewController.swift @@ -196,7 +196,7 @@ extension CountryPickerViewController { // MARK:- UISearchResultsUpdating extension CountryPickerViewController: UISearchResultsUpdating { - public func updateSearchResults(for searchController: UISearchController) { + func updateSearchResults(for searchController: UISearchController) { isSearchMode = false if let text = searchController.searchBar.text, text.count > 0 { isSearchMode = true @@ -220,13 +220,13 @@ extension CountryPickerViewController: UISearchResultsUpdating { // MARK:- UISearchBarDelegate extension CountryPickerViewController: UISearchBarDelegate { - public func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { + func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { // Hide the back/left navigationItem button navigationItem.leftBarButtonItem = nil navigationItem.hidesBackButton = true } - public func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { + func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { // Show the back/left navigationItem button prepareNavItem() navigationItem.hidesBackButton = false diff --git a/CountryPickerView/Delegate+DataSource.swift b/CountryPickerView/Delegate+DataSource.swift index efb0886..ae03e21 100644 --- a/CountryPickerView/Delegate+DataSource.swift +++ b/CountryPickerView/Delegate+DataSource.swift @@ -11,6 +11,16 @@ import Foundation public protocol CountryPickerViewDelegate: class { /// Called when the user selects a country from the list. func countryPickerView(_ countryPickerView: CountryPickerView, didSelectCountry country: Country) + + /// Called before the internal UITableViewController is presented or pushed. + /// If the presenting view controller is not a UINavigationController, the UITableViewController + /// is embedded in a UINavigationController. + func countryPickerView(_ countryPickerView: CountryPickerView, willShow viewController: UITableViewController) + + /// Called after the internal UITableViewController is presented or pushed. + /// If the presenting view controller is not a UINavigationController, the UITableViewController + /// is embedded in a UINavigationController. + func countryPickerView(_ countryPickerView: CountryPickerView, didShow viewController: UITableViewController) } public protocol CountryPickerViewDataSource: class { @@ -72,3 +82,18 @@ public extension CountryPickerViewDataSource { return false } } + + +// MARK:- CountryPickerViewDelegate default implementations +public extension CountryPickerViewDelegate { + + func countryPickerView(_ countryPickerView: CountryPickerView, willShow viewController: UITableViewController) { + + } + + func countryPickerView(_ countryPickerView: CountryPickerView, didShow viewController: UITableViewController) { + + } + +} + diff --git a/CountryPickerView/Extensions.swift b/CountryPickerView/Extensions.swift index 17bed69..60d2b75 100644 --- a/CountryPickerView/Extensions.swift +++ b/CountryPickerView/Extensions.swift @@ -32,4 +32,16 @@ extension UINavigationController { completion() } } + + func pushViewController(_ viewController: UIViewController, animated: Bool, completion: @escaping () -> Void) { + pushViewController(viewController, animated: animated) + + if animated, let coordinator = transitionCoordinator { + coordinator.animate(alongsideTransition: nil) { _ in + completion() + } + } else { + completion() + } + } } From 4f4af27c3f4e0c5d6c6f3eec321287708bac2fa0 Mon Sep 17 00:00:00 2001 From: Kizito Nwose Date: Tue, 12 Jun 2018 11:38:58 +0100 Subject: [PATCH 2/3] Fix an issue where the search bar goes off screen sometimes. --- .../CountryPickerViewController.swift | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/CountryPickerView/CountryPickerViewController.swift b/CountryPickerView/CountryPickerViewController.swift index fc69545..6d25265 100644 --- a/CountryPickerView/CountryPickerViewController.swift +++ b/CountryPickerView/CountryPickerViewController.swift @@ -35,7 +35,6 @@ class CountryPickerViewController: UITableViewController { } - // UI Setup extension CountryPickerViewController { @@ -104,7 +103,9 @@ extension CountryPickerViewController { searchController?.searchResultsUpdater = self searchController?.dimsBackgroundDuringPresentation = false searchController?.hidesNavigationBarDuringPresentation = searchBarPosition == .tableViewHeader + searchController?.definesPresentationContext = true searchController?.searchBar.delegate = self + searchController?.delegate = self switch searchBarPosition { case .tableViewHeader: tableView.tableHeaderView = searchController?.searchBar @@ -118,7 +119,6 @@ extension CountryPickerViewController { } } - //MARK:- UITableViewDataSource extension CountryPickerViewController { @@ -164,7 +164,6 @@ extension CountryPickerViewController { } } - //MARK:- UITableViewDelegate extension CountryPickerViewController { @@ -193,7 +192,6 @@ extension CountryPickerViewController { } } - // MARK:- UISearchResultsUpdating extension CountryPickerViewController: UISearchResultsUpdating { func updateSearchResults(for searchController: UISearchController) { @@ -217,7 +215,6 @@ extension CountryPickerViewController: UISearchResultsUpdating { } } - // MARK:- UISearchBarDelegate extension CountryPickerViewController: UISearchBarDelegate { func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { @@ -234,4 +231,15 @@ extension CountryPickerViewController: UISearchBarDelegate { } +// MARK:- UISearchControllerDelegate +// Fixes an issue where the search bar goes off screen sometimes. +extension CountryPickerViewController: UISearchControllerDelegate { + func willPresentSearchController(_ searchController: UISearchController) { + self.navigationController?.navigationBar.isTranslucent = true + } + + func willDismissSearchController(_ searchController: UISearchController) { + self.navigationController?.navigationBar.isTranslucent = false + } +} From 349625c2e4b2cd3d41259a220ec6ea7f4b968b30 Mon Sep 17 00:00:00 2001 From: Kizito Nwose Date: Sun, 1 Jul 2018 13:33:53 +0100 Subject: [PATCH 3/3] Update demo project. --- .../project.pbxproj | 20 ++----------------- .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++++++++ CountryPickerViewDemo/Podfile.lock | 6 +++--- 3 files changed, 13 insertions(+), 21 deletions(-) create mode 100644 CountryPickerViewDemo/CountryPickerViewDemo.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/CountryPickerViewDemo/CountryPickerViewDemo.xcodeproj/project.pbxproj b/CountryPickerViewDemo/CountryPickerViewDemo.xcodeproj/project.pbxproj index a6f491c..d9887ec 100644 --- a/CountryPickerViewDemo/CountryPickerViewDemo.xcodeproj/project.pbxproj +++ b/CountryPickerViewDemo/CountryPickerViewDemo.xcodeproj/project.pbxproj @@ -113,7 +113,6 @@ 004CA2FC1F705DD600B690B8 /* Frameworks */, 004CA2FD1F705DD600B690B8 /* Resources */, D073A94C45743AB4CB4D944D /* [CP] Embed Pods Frameworks */, - B81A81F5EA364DFEE7FEEDCA /* [CP] Copy Pods Resources */, 509720AE1FDEE189001244B0 /* Embed Frameworks */, ); buildRules = ( @@ -174,21 +173,6 @@ /* End PBXResourcesBuildPhase section */ /* Begin PBXShellScriptBuildPhase section */ - B81A81F5EA364DFEE7FEEDCA /* [CP] Copy Pods Resources */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "[CP] Copy Pods Resources"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-CountryPickerViewDemo/Pods-CountryPickerViewDemo-resources.sh\"\n"; - showEnvVarsInLog = 0; - }; D073A94C45743AB4CB4D944D /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; @@ -372,7 +356,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 459706724723319B379BF2E4 /* Pods-CountryPickerViewDemo.debug.xcconfig */; buildSettings = { - ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = "$(inherited)"; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; DEVELOPMENT_TEAM = K3JWY7DT4K; INFOPLIST_FILE = CountryPickerViewDemo/Info.plist; @@ -388,7 +372,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 476008F23554AD41FFB937C1 /* Pods-CountryPickerViewDemo.release.xcconfig */; buildSettings = { - ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = "$(inherited)"; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; DEVELOPMENT_TEAM = K3JWY7DT4K; INFOPLIST_FILE = CountryPickerViewDemo/Info.plist; diff --git a/CountryPickerViewDemo/CountryPickerViewDemo.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/CountryPickerViewDemo/CountryPickerViewDemo.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/CountryPickerViewDemo/CountryPickerViewDemo.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/CountryPickerViewDemo/Podfile.lock b/CountryPickerViewDemo/Podfile.lock index d0e27a9..823447c 100644 --- a/CountryPickerViewDemo/Podfile.lock +++ b/CountryPickerViewDemo/Podfile.lock @@ -6,11 +6,11 @@ DEPENDENCIES: EXTERNAL SOURCES: CountryPickerView: - :path: ../ + :path: "../" SPEC CHECKSUMS: - CountryPickerView: db05b3dbd2c6bd4c04c388ee53b5bedf8d84a5fd + CountryPickerView: f23e96fe13908b00b861d5dd18c3688632500bdc PODFILE CHECKSUM: 649829ad9a32abd953e33364dc48a4a40a615047 -COCOAPODS: 1.3.1 +COCOAPODS: 1.5.3