├── .gitignore ├── .swift-version ├── Demo.gif ├── DynamicTabBarController.podspec ├── DynamicTabBarController.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcuserdata │ └── nathantannar.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist ├── DynamicTabBarController ├── DynamicTabBar.swift ├── DynamicTabBarCell.swift ├── DynamicTabBarController.h ├── DynamicTabBarController.swift ├── Info.plist ├── NSConstraintLayoutSet.swift └── UIView+AutoLayout.swift ├── Example ├── AppDelegate.swift ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ ├── Contents.json │ ├── apple.imageset │ │ ├── Contents.json │ │ └── icons8-apple_filled.png │ ├── banana.imageset │ │ ├── Contents.json │ │ └── icons8-banana_filled.png │ ├── grapes.imageset │ │ ├── Contents.json │ │ └── icons8-grapes_filled.png │ ├── pear.imageset │ │ ├── Contents.json │ │ └── icons8-pear_filled.png │ ├── plum.imageset │ │ ├── Contents.json │ │ └── icons8-plum_filled.png │ └── pumpkin.imageset │ │ ├── Contents.json │ │ └── icons8-pumpkin_filled.png ├── Base.lproj │ └── LaunchScreen.storyboard ├── Fruit.swift ├── Info.plist ├── TableViewController.swift ├── UIView+AutoLayout.swift └── ViewController.swift ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData 8 | Example/.DS_Store 9 | 10 | ## Various settings 11 | *.pbxuser 12 | !default.pbxuser 13 | *.mode1v3 14 | !default.mode1v3 15 | *.mode2v3 16 | !default.mode2v3 17 | *.perspectivev3 18 | !default.perspectivev3 19 | xcuserdata 20 | 21 | ## Other 22 | *.xccheckout 23 | *.moved-aside 24 | *.xcuserstate 25 | *.xcscmblueprint 26 | 27 | ## Obj-C/Swift specific 28 | *.hmap 29 | *.ipa 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | -------------------------------------------------------------------------------- /.swift-version: -------------------------------------------------------------------------------- 1 | 4.0 2 | -------------------------------------------------------------------------------- /Demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nathantannar4/DynamicTabBarController/cfd4415124c97db01b36c8acd47195c32785ba7d/Demo.gif -------------------------------------------------------------------------------- /DynamicTabBarController.podspec: -------------------------------------------------------------------------------- 1 | 2 | Pod::Spec.new do |s| 3 | 4 | # 1 - Specs 5 | s.platform = :ios 6 | s.ios.deployment_target = '9.0' 7 | s.name = 'DynamicTabBarController' 8 | s.summary = "A dynamic UITabBarController" 9 | s.description = "A must use TabBarController if you plan on dynamically changing the ViewControllers or want a TabBarController similar to other AAA apps" 10 | s.requires_arc = true 11 | s.pod_target_xcconfig = { 'SWIFT_VERSION' => '4.0' } 12 | 13 | # 2 - Version 14 | s.version = "1.0.1" 15 | 16 | # 3 - License 17 | s.license = { :type => "MIT", :file => "LICENSE" } 18 | 19 | # 4 - Author 20 | s.author = { "Nathan Tannar" => "nathantannar4@gmail.com" } 21 | 22 | # 5 - Homepage 23 | s.homepage = "https://github.com/nathantannar4/DynamicTabBarController" 24 | 25 | # 6 - Source 26 | s.source = { :git => "https://github.com/nathantannar4/DynamicTabBarController.git", :tag => "#{s.version}"} 27 | 28 | # 7 - Dependencies 29 | s.framework = "UIKit" 30 | 31 | # 8 - Source Files 32 | s.source_files = "DynamicTabBarController/**/*.{swift}" 33 | 34 | # 9 - Resources 35 | 36 | end 37 | -------------------------------------------------------------------------------- /DynamicTabBarController.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 3833C49220367CDA00E4D301 /* TableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3833C49120367CDA00E4D301 /* TableViewController.swift */; }; 11 | 3861787E1FBC21750059FE89 /* DynamicTabBarController.h in Headers */ = {isa = PBXBuildFile; fileRef = 3861787C1FBC21750059FE89 /* DynamicTabBarController.h */; settings = {ATTRIBUTES = (Public, ); }; }; 12 | 386178871FBC218C0059FE89 /* DynamicTabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 386178841FBC218B0059FE89 /* DynamicTabBarController.swift */; }; 13 | 386178881FBC218C0059FE89 /* DynamicTabBarCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 386178851FBC218B0059FE89 /* DynamicTabBarCell.swift */; }; 14 | 386178891FBC218C0059FE89 /* DynamicTabBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 386178861FBC218C0059FE89 /* DynamicTabBar.swift */; }; 15 | 3861788C1FBC21970059FE89 /* UIView+AutoLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3861788A1FBC21970059FE89 /* UIView+AutoLayout.swift */; }; 16 | 3861788D1FBC21970059FE89 /* NSConstraintLayoutSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3861788B1FBC21970059FE89 /* NSConstraintLayoutSet.swift */; }; 17 | 386178951FBC21C60059FE89 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 386178941FBC21C60059FE89 /* AppDelegate.swift */; }; 18 | 3861789C1FBC21C60059FE89 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3861789B1FBC21C60059FE89 /* Assets.xcassets */; }; 19 | 3861789F1FBC21C60059FE89 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3861789D1FBC21C60059FE89 /* LaunchScreen.storyboard */; }; 20 | 386178A51FBC21EB0059FE89 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 386178A41FBC21EB0059FE89 /* ViewController.swift */; }; 21 | 386178A71FBC21F10059FE89 /* Fruit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 386178A61FBC21F10059FE89 /* Fruit.swift */; }; 22 | 386178A91FBC22E10059FE89 /* UIView+AutoLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 386178A81FBC22E10059FE89 /* UIView+AutoLayout.swift */; }; 23 | 386178AA1FBC23760059FE89 /* DynamicTabBarController.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 386178791FBC21740059FE89 /* DynamicTabBarController.framework */; }; 24 | 386178AB1FBC23760059FE89 /* DynamicTabBarController.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 386178791FBC21740059FE89 /* DynamicTabBarController.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 25 | /* End PBXBuildFile section */ 26 | 27 | /* Begin PBXContainerItemProxy section */ 28 | 386178AC1FBC23760059FE89 /* PBXContainerItemProxy */ = { 29 | isa = PBXContainerItemProxy; 30 | containerPortal = 386178701FBC21740059FE89 /* Project object */; 31 | proxyType = 1; 32 | remoteGlobalIDString = 386178781FBC21740059FE89; 33 | remoteInfo = DynamicTabBarController; 34 | }; 35 | /* End PBXContainerItemProxy section */ 36 | 37 | /* Begin PBXCopyFilesBuildPhase section */ 38 | 386178AE1FBC23760059FE89 /* Embed Frameworks */ = { 39 | isa = PBXCopyFilesBuildPhase; 40 | buildActionMask = 2147483647; 41 | dstPath = ""; 42 | dstSubfolderSpec = 10; 43 | files = ( 44 | 386178AB1FBC23760059FE89 /* DynamicTabBarController.framework in Embed Frameworks */, 45 | ); 46 | name = "Embed Frameworks"; 47 | runOnlyForDeploymentPostprocessing = 0; 48 | }; 49 | /* End PBXCopyFilesBuildPhase section */ 50 | 51 | /* Begin PBXFileReference section */ 52 | 3833C49120367CDA00E4D301 /* TableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewController.swift; sourceTree = ""; }; 53 | 3833C49320367D6F00E4D301 /* DynamicTabBarController.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; path = DynamicTabBarController.podspec; sourceTree = ""; }; 54 | 3833C49420367D6F00E4D301 /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENSE; sourceTree = ""; }; 55 | 3833C49520367D6F00E4D301 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 56 | 386178791FBC21740059FE89 /* DynamicTabBarController.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = DynamicTabBarController.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 57 | 3861787C1FBC21750059FE89 /* DynamicTabBarController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DynamicTabBarController.h; sourceTree = ""; }; 58 | 3861787D1FBC21750059FE89 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 59 | 386178841FBC218B0059FE89 /* DynamicTabBarController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DynamicTabBarController.swift; sourceTree = ""; }; 60 | 386178851FBC218B0059FE89 /* DynamicTabBarCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DynamicTabBarCell.swift; sourceTree = ""; }; 61 | 386178861FBC218C0059FE89 /* DynamicTabBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DynamicTabBar.swift; sourceTree = ""; }; 62 | 3861788A1FBC21970059FE89 /* UIView+AutoLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+AutoLayout.swift"; sourceTree = ""; }; 63 | 3861788B1FBC21970059FE89 /* NSConstraintLayoutSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSConstraintLayoutSet.swift; sourceTree = ""; }; 64 | 386178921FBC21C60059FE89 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; }; 65 | 386178941FBC21C60059FE89 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 66 | 3861789B1FBC21C60059FE89 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 67 | 3861789E1FBC21C60059FE89 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 68 | 386178A01FBC21C60059FE89 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 69 | 386178A41FBC21EB0059FE89 /* ViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 70 | 386178A61FBC21F10059FE89 /* Fruit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Fruit.swift; sourceTree = ""; }; 71 | 386178A81FBC22E10059FE89 /* UIView+AutoLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+AutoLayout.swift"; sourceTree = ""; }; 72 | /* End PBXFileReference section */ 73 | 74 | /* Begin PBXFrameworksBuildPhase section */ 75 | 386178751FBC21740059FE89 /* Frameworks */ = { 76 | isa = PBXFrameworksBuildPhase; 77 | buildActionMask = 2147483647; 78 | files = ( 79 | ); 80 | runOnlyForDeploymentPostprocessing = 0; 81 | }; 82 | 3861788F1FBC21C60059FE89 /* Frameworks */ = { 83 | isa = PBXFrameworksBuildPhase; 84 | buildActionMask = 2147483647; 85 | files = ( 86 | 386178AA1FBC23760059FE89 /* DynamicTabBarController.framework in Frameworks */, 87 | ); 88 | runOnlyForDeploymentPostprocessing = 0; 89 | }; 90 | /* End PBXFrameworksBuildPhase section */ 91 | 92 | /* Begin PBXGroup section */ 93 | 3861786F1FBC21740059FE89 = { 94 | isa = PBXGroup; 95 | children = ( 96 | 3833C49420367D6F00E4D301 /* LICENSE */, 97 | 3833C49520367D6F00E4D301 /* README.md */, 98 | 3833C49320367D6F00E4D301 /* DynamicTabBarController.podspec */, 99 | 3861787B1FBC21750059FE89 /* DynamicTabBarController */, 100 | 386178931FBC21C60059FE89 /* Example */, 101 | 3861787A1FBC21740059FE89 /* Products */, 102 | ); 103 | sourceTree = ""; 104 | }; 105 | 3861787A1FBC21740059FE89 /* Products */ = { 106 | isa = PBXGroup; 107 | children = ( 108 | 386178791FBC21740059FE89 /* DynamicTabBarController.framework */, 109 | 386178921FBC21C60059FE89 /* Example.app */, 110 | ); 111 | name = Products; 112 | sourceTree = ""; 113 | }; 114 | 3861787B1FBC21750059FE89 /* DynamicTabBarController */ = { 115 | isa = PBXGroup; 116 | children = ( 117 | 3861787C1FBC21750059FE89 /* DynamicTabBarController.h */, 118 | 386178861FBC218C0059FE89 /* DynamicTabBar.swift */, 119 | 386178851FBC218B0059FE89 /* DynamicTabBarCell.swift */, 120 | 386178841FBC218B0059FE89 /* DynamicTabBarController.swift */, 121 | 3861788B1FBC21970059FE89 /* NSConstraintLayoutSet.swift */, 122 | 3861788A1FBC21970059FE89 /* UIView+AutoLayout.swift */, 123 | 3861787D1FBC21750059FE89 /* Info.plist */, 124 | ); 125 | path = DynamicTabBarController; 126 | sourceTree = ""; 127 | }; 128 | 386178931FBC21C60059FE89 /* Example */ = { 129 | isa = PBXGroup; 130 | children = ( 131 | 386178941FBC21C60059FE89 /* AppDelegate.swift */, 132 | 386178A61FBC21F10059FE89 /* Fruit.swift */, 133 | 386178A41FBC21EB0059FE89 /* ViewController.swift */, 134 | 3833C49120367CDA00E4D301 /* TableViewController.swift */, 135 | 386178A81FBC22E10059FE89 /* UIView+AutoLayout.swift */, 136 | 3861789B1FBC21C60059FE89 /* Assets.xcassets */, 137 | 3861789D1FBC21C60059FE89 /* LaunchScreen.storyboard */, 138 | 386178A01FBC21C60059FE89 /* Info.plist */, 139 | ); 140 | path = Example; 141 | sourceTree = ""; 142 | }; 143 | /* End PBXGroup section */ 144 | 145 | /* Begin PBXHeadersBuildPhase section */ 146 | 386178761FBC21740059FE89 /* Headers */ = { 147 | isa = PBXHeadersBuildPhase; 148 | buildActionMask = 2147483647; 149 | files = ( 150 | 3861787E1FBC21750059FE89 /* DynamicTabBarController.h in Headers */, 151 | ); 152 | runOnlyForDeploymentPostprocessing = 0; 153 | }; 154 | /* End PBXHeadersBuildPhase section */ 155 | 156 | /* Begin PBXNativeTarget section */ 157 | 386178781FBC21740059FE89 /* DynamicTabBarController */ = { 158 | isa = PBXNativeTarget; 159 | buildConfigurationList = 386178811FBC21750059FE89 /* Build configuration list for PBXNativeTarget "DynamicTabBarController" */; 160 | buildPhases = ( 161 | 386178741FBC21740059FE89 /* Sources */, 162 | 386178751FBC21740059FE89 /* Frameworks */, 163 | 386178761FBC21740059FE89 /* Headers */, 164 | 386178771FBC21740059FE89 /* Resources */, 165 | ); 166 | buildRules = ( 167 | ); 168 | dependencies = ( 169 | ); 170 | name = DynamicTabBarController; 171 | productName = DynamicTabBarController; 172 | productReference = 386178791FBC21740059FE89 /* DynamicTabBarController.framework */; 173 | productType = "com.apple.product-type.framework"; 174 | }; 175 | 386178911FBC21C60059FE89 /* Example */ = { 176 | isa = PBXNativeTarget; 177 | buildConfigurationList = 386178A11FBC21C60059FE89 /* Build configuration list for PBXNativeTarget "Example" */; 178 | buildPhases = ( 179 | 3861788E1FBC21C60059FE89 /* Sources */, 180 | 3861788F1FBC21C60059FE89 /* Frameworks */, 181 | 386178901FBC21C60059FE89 /* Resources */, 182 | 386178AE1FBC23760059FE89 /* Embed Frameworks */, 183 | ); 184 | buildRules = ( 185 | ); 186 | dependencies = ( 187 | 386178AD1FBC23760059FE89 /* PBXTargetDependency */, 188 | ); 189 | name = Example; 190 | productName = Example; 191 | productReference = 386178921FBC21C60059FE89 /* Example.app */; 192 | productType = "com.apple.product-type.application"; 193 | }; 194 | /* End PBXNativeTarget section */ 195 | 196 | /* Begin PBXProject section */ 197 | 386178701FBC21740059FE89 /* Project object */ = { 198 | isa = PBXProject; 199 | attributes = { 200 | LastSwiftUpdateCheck = 0910; 201 | LastUpgradeCheck = 0910; 202 | ORGANIZATIONNAME = "Nathan Tannar"; 203 | TargetAttributes = { 204 | 386178781FBC21740059FE89 = { 205 | CreatedOnToolsVersion = 9.1; 206 | LastSwiftMigration = 1000; 207 | ProvisioningStyle = Automatic; 208 | }; 209 | 386178911FBC21C60059FE89 = { 210 | CreatedOnToolsVersion = 9.1; 211 | LastSwiftMigration = 1000; 212 | ProvisioningStyle = Automatic; 213 | }; 214 | }; 215 | }; 216 | buildConfigurationList = 386178731FBC21740059FE89 /* Build configuration list for PBXProject "DynamicTabBarController" */; 217 | compatibilityVersion = "Xcode 8.0"; 218 | developmentRegion = en; 219 | hasScannedForEncodings = 0; 220 | knownRegions = ( 221 | en, 222 | Base, 223 | ); 224 | mainGroup = 3861786F1FBC21740059FE89; 225 | productRefGroup = 3861787A1FBC21740059FE89 /* Products */; 226 | projectDirPath = ""; 227 | projectRoot = ""; 228 | targets = ( 229 | 386178781FBC21740059FE89 /* DynamicTabBarController */, 230 | 386178911FBC21C60059FE89 /* Example */, 231 | ); 232 | }; 233 | /* End PBXProject section */ 234 | 235 | /* Begin PBXResourcesBuildPhase section */ 236 | 386178771FBC21740059FE89 /* Resources */ = { 237 | isa = PBXResourcesBuildPhase; 238 | buildActionMask = 2147483647; 239 | files = ( 240 | ); 241 | runOnlyForDeploymentPostprocessing = 0; 242 | }; 243 | 386178901FBC21C60059FE89 /* Resources */ = { 244 | isa = PBXResourcesBuildPhase; 245 | buildActionMask = 2147483647; 246 | files = ( 247 | 3861789F1FBC21C60059FE89 /* LaunchScreen.storyboard in Resources */, 248 | 3861789C1FBC21C60059FE89 /* Assets.xcassets in Resources */, 249 | ); 250 | runOnlyForDeploymentPostprocessing = 0; 251 | }; 252 | /* End PBXResourcesBuildPhase section */ 253 | 254 | /* Begin PBXSourcesBuildPhase section */ 255 | 386178741FBC21740059FE89 /* Sources */ = { 256 | isa = PBXSourcesBuildPhase; 257 | buildActionMask = 2147483647; 258 | files = ( 259 | 386178891FBC218C0059FE89 /* DynamicTabBar.swift in Sources */, 260 | 3861788C1FBC21970059FE89 /* UIView+AutoLayout.swift in Sources */, 261 | 3861788D1FBC21970059FE89 /* NSConstraintLayoutSet.swift in Sources */, 262 | 386178871FBC218C0059FE89 /* DynamicTabBarController.swift in Sources */, 263 | 386178881FBC218C0059FE89 /* DynamicTabBarCell.swift in Sources */, 264 | ); 265 | runOnlyForDeploymentPostprocessing = 0; 266 | }; 267 | 3861788E1FBC21C60059FE89 /* Sources */ = { 268 | isa = PBXSourcesBuildPhase; 269 | buildActionMask = 2147483647; 270 | files = ( 271 | 386178A71FBC21F10059FE89 /* Fruit.swift in Sources */, 272 | 386178A91FBC22E10059FE89 /* UIView+AutoLayout.swift in Sources */, 273 | 386178A51FBC21EB0059FE89 /* ViewController.swift in Sources */, 274 | 3833C49220367CDA00E4D301 /* TableViewController.swift in Sources */, 275 | 386178951FBC21C60059FE89 /* AppDelegate.swift in Sources */, 276 | ); 277 | runOnlyForDeploymentPostprocessing = 0; 278 | }; 279 | /* End PBXSourcesBuildPhase section */ 280 | 281 | /* Begin PBXTargetDependency section */ 282 | 386178AD1FBC23760059FE89 /* PBXTargetDependency */ = { 283 | isa = PBXTargetDependency; 284 | target = 386178781FBC21740059FE89 /* DynamicTabBarController */; 285 | targetProxy = 386178AC1FBC23760059FE89 /* PBXContainerItemProxy */; 286 | }; 287 | /* End PBXTargetDependency section */ 288 | 289 | /* Begin PBXVariantGroup section */ 290 | 3861789D1FBC21C60059FE89 /* LaunchScreen.storyboard */ = { 291 | isa = PBXVariantGroup; 292 | children = ( 293 | 3861789E1FBC21C60059FE89 /* Base */, 294 | ); 295 | name = LaunchScreen.storyboard; 296 | sourceTree = ""; 297 | }; 298 | /* End PBXVariantGroup section */ 299 | 300 | /* Begin XCBuildConfiguration section */ 301 | 3861787F1FBC21750059FE89 /* Debug */ = { 302 | isa = XCBuildConfiguration; 303 | buildSettings = { 304 | ALWAYS_SEARCH_USER_PATHS = NO; 305 | CLANG_ANALYZER_NONNULL = YES; 306 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 307 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 308 | CLANG_CXX_LIBRARY = "libc++"; 309 | CLANG_ENABLE_MODULES = YES; 310 | CLANG_ENABLE_OBJC_ARC = YES; 311 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 312 | CLANG_WARN_BOOL_CONVERSION = YES; 313 | CLANG_WARN_COMMA = YES; 314 | CLANG_WARN_CONSTANT_CONVERSION = YES; 315 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 316 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 317 | CLANG_WARN_EMPTY_BODY = YES; 318 | CLANG_WARN_ENUM_CONVERSION = YES; 319 | CLANG_WARN_INFINITE_RECURSION = YES; 320 | CLANG_WARN_INT_CONVERSION = YES; 321 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 322 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 323 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 324 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 325 | CLANG_WARN_STRICT_PROTOTYPES = YES; 326 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 327 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 328 | CLANG_WARN_UNREACHABLE_CODE = YES; 329 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 330 | CODE_SIGN_IDENTITY = "iPhone Developer"; 331 | COPY_PHASE_STRIP = NO; 332 | CURRENT_PROJECT_VERSION = 1; 333 | DEBUG_INFORMATION_FORMAT = dwarf; 334 | ENABLE_STRICT_OBJC_MSGSEND = YES; 335 | ENABLE_TESTABILITY = YES; 336 | GCC_C_LANGUAGE_STANDARD = gnu11; 337 | GCC_DYNAMIC_NO_PIC = NO; 338 | GCC_NO_COMMON_BLOCKS = YES; 339 | GCC_OPTIMIZATION_LEVEL = 0; 340 | GCC_PREPROCESSOR_DEFINITIONS = ( 341 | "DEBUG=1", 342 | "$(inherited)", 343 | ); 344 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 345 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 346 | GCC_WARN_UNDECLARED_SELECTOR = YES; 347 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 348 | GCC_WARN_UNUSED_FUNCTION = YES; 349 | GCC_WARN_UNUSED_VARIABLE = YES; 350 | IPHONEOS_DEPLOYMENT_TARGET = 11.1; 351 | MTL_ENABLE_DEBUG_INFO = YES; 352 | ONLY_ACTIVE_ARCH = YES; 353 | SDKROOT = iphoneos; 354 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 355 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 356 | VERSIONING_SYSTEM = "apple-generic"; 357 | VERSION_INFO_PREFIX = ""; 358 | }; 359 | name = Debug; 360 | }; 361 | 386178801FBC21750059FE89 /* Release */ = { 362 | isa = XCBuildConfiguration; 363 | buildSettings = { 364 | ALWAYS_SEARCH_USER_PATHS = NO; 365 | CLANG_ANALYZER_NONNULL = YES; 366 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 367 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 368 | CLANG_CXX_LIBRARY = "libc++"; 369 | CLANG_ENABLE_MODULES = YES; 370 | CLANG_ENABLE_OBJC_ARC = YES; 371 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 372 | CLANG_WARN_BOOL_CONVERSION = YES; 373 | CLANG_WARN_COMMA = YES; 374 | CLANG_WARN_CONSTANT_CONVERSION = YES; 375 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 376 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 377 | CLANG_WARN_EMPTY_BODY = YES; 378 | CLANG_WARN_ENUM_CONVERSION = YES; 379 | CLANG_WARN_INFINITE_RECURSION = YES; 380 | CLANG_WARN_INT_CONVERSION = YES; 381 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 382 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 383 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 384 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 385 | CLANG_WARN_STRICT_PROTOTYPES = YES; 386 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 387 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 388 | CLANG_WARN_UNREACHABLE_CODE = YES; 389 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 390 | CODE_SIGN_IDENTITY = "iPhone Developer"; 391 | COPY_PHASE_STRIP = NO; 392 | CURRENT_PROJECT_VERSION = 1; 393 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 394 | ENABLE_NS_ASSERTIONS = NO; 395 | ENABLE_STRICT_OBJC_MSGSEND = YES; 396 | GCC_C_LANGUAGE_STANDARD = gnu11; 397 | GCC_NO_COMMON_BLOCKS = YES; 398 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 399 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 400 | GCC_WARN_UNDECLARED_SELECTOR = YES; 401 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 402 | GCC_WARN_UNUSED_FUNCTION = YES; 403 | GCC_WARN_UNUSED_VARIABLE = YES; 404 | IPHONEOS_DEPLOYMENT_TARGET = 11.1; 405 | MTL_ENABLE_DEBUG_INFO = NO; 406 | SDKROOT = iphoneos; 407 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 408 | VALIDATE_PRODUCT = YES; 409 | VERSIONING_SYSTEM = "apple-generic"; 410 | VERSION_INFO_PREFIX = ""; 411 | }; 412 | name = Release; 413 | }; 414 | 386178821FBC21750059FE89 /* Debug */ = { 415 | isa = XCBuildConfiguration; 416 | buildSettings = { 417 | CLANG_ENABLE_MODULES = YES; 418 | CODE_SIGN_IDENTITY = ""; 419 | CODE_SIGN_STYLE = Automatic; 420 | DEFINES_MODULE = YES; 421 | DEVELOPMENT_TEAM = 9EUCWD26TP; 422 | DYLIB_COMPATIBILITY_VERSION = 1; 423 | DYLIB_CURRENT_VERSION = 1; 424 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 425 | INFOPLIST_FILE = DynamicTabBarController/Info.plist; 426 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 427 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 428 | PRODUCT_BUNDLE_IDENTIFIER = me.nathantannar.DynamicTabBarController; 429 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 430 | SKIP_INSTALL = YES; 431 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 432 | SWIFT_VERSION = 4.2; 433 | TARGETED_DEVICE_FAMILY = "1,2"; 434 | }; 435 | name = Debug; 436 | }; 437 | 386178831FBC21750059FE89 /* Release */ = { 438 | isa = XCBuildConfiguration; 439 | buildSettings = { 440 | CLANG_ENABLE_MODULES = YES; 441 | CODE_SIGN_IDENTITY = ""; 442 | CODE_SIGN_STYLE = Automatic; 443 | DEFINES_MODULE = YES; 444 | DEVELOPMENT_TEAM = 9EUCWD26TP; 445 | DYLIB_COMPATIBILITY_VERSION = 1; 446 | DYLIB_CURRENT_VERSION = 1; 447 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 448 | INFOPLIST_FILE = DynamicTabBarController/Info.plist; 449 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 450 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 451 | PRODUCT_BUNDLE_IDENTIFIER = me.nathantannar.DynamicTabBarController; 452 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 453 | SKIP_INSTALL = YES; 454 | SWIFT_VERSION = 4.2; 455 | TARGETED_DEVICE_FAMILY = "1,2"; 456 | }; 457 | name = Release; 458 | }; 459 | 386178A21FBC21C60059FE89 /* Debug */ = { 460 | isa = XCBuildConfiguration; 461 | buildSettings = { 462 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 463 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 464 | CODE_SIGN_STYLE = Automatic; 465 | DEVELOPMENT_TEAM = 9EUCWD26TP; 466 | INFOPLIST_FILE = Example/Info.plist; 467 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 468 | PRODUCT_BUNDLE_IDENTIFIER = me.nathantannar.Example; 469 | PRODUCT_NAME = "$(TARGET_NAME)"; 470 | SWIFT_VERSION = 4.2; 471 | TARGETED_DEVICE_FAMILY = "1,2"; 472 | }; 473 | name = Debug; 474 | }; 475 | 386178A31FBC21C60059FE89 /* Release */ = { 476 | isa = XCBuildConfiguration; 477 | buildSettings = { 478 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 479 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 480 | CODE_SIGN_STYLE = Automatic; 481 | DEVELOPMENT_TEAM = 9EUCWD26TP; 482 | INFOPLIST_FILE = Example/Info.plist; 483 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 484 | PRODUCT_BUNDLE_IDENTIFIER = me.nathantannar.Example; 485 | PRODUCT_NAME = "$(TARGET_NAME)"; 486 | SWIFT_VERSION = 4.2; 487 | TARGETED_DEVICE_FAMILY = "1,2"; 488 | }; 489 | name = Release; 490 | }; 491 | /* End XCBuildConfiguration section */ 492 | 493 | /* Begin XCConfigurationList section */ 494 | 386178731FBC21740059FE89 /* Build configuration list for PBXProject "DynamicTabBarController" */ = { 495 | isa = XCConfigurationList; 496 | buildConfigurations = ( 497 | 3861787F1FBC21750059FE89 /* Debug */, 498 | 386178801FBC21750059FE89 /* Release */, 499 | ); 500 | defaultConfigurationIsVisible = 0; 501 | defaultConfigurationName = Release; 502 | }; 503 | 386178811FBC21750059FE89 /* Build configuration list for PBXNativeTarget "DynamicTabBarController" */ = { 504 | isa = XCConfigurationList; 505 | buildConfigurations = ( 506 | 386178821FBC21750059FE89 /* Debug */, 507 | 386178831FBC21750059FE89 /* Release */, 508 | ); 509 | defaultConfigurationIsVisible = 0; 510 | defaultConfigurationName = Release; 511 | }; 512 | 386178A11FBC21C60059FE89 /* Build configuration list for PBXNativeTarget "Example" */ = { 513 | isa = XCConfigurationList; 514 | buildConfigurations = ( 515 | 386178A21FBC21C60059FE89 /* Debug */, 516 | 386178A31FBC21C60059FE89 /* Release */, 517 | ); 518 | defaultConfigurationIsVisible = 0; 519 | defaultConfigurationName = Release; 520 | }; 521 | /* End XCConfigurationList section */ 522 | }; 523 | rootObject = 386178701FBC21740059FE89 /* Project object */; 524 | } 525 | -------------------------------------------------------------------------------- /DynamicTabBarController.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DynamicTabBarController.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /DynamicTabBarController.xcodeproj/xcuserdata/nathantannar.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | DynamicTabBarController.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | Example.xcscheme 13 | 14 | orderHint 15 | 1 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /DynamicTabBarController/DynamicTabBar.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DynamicTabBar.swift 3 | // DynamicTabBarController 4 | // 5 | // Copyright © 2017 Nathan Tannar. 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | // 25 | // Created by Nathan Tannar on 10/28/17. 26 | // 27 | 28 | import UIKit 29 | 30 | final public class DynamicTabBar: UIView { 31 | 32 | public enum ScrollIndicatorPosition { 33 | case top, bottom 34 | } 35 | 36 | // MARK: - Properties [Public] 37 | 38 | public var activeTintColor = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1) { 39 | didSet { 40 | scrollIndicator.backgroundColor = activeTintColor 41 | collectionView.reloadData() 42 | } 43 | } 44 | 45 | public var inactiveTintColor = UIColor.lightGray { 46 | didSet { 47 | collectionView.reloadData() 48 | } 49 | } 50 | 51 | public var scrollIndicatorPosition: ScrollIndicatorPosition = .top { 52 | didSet { 53 | updateScrollIndicatorPosition() 54 | } 55 | } 56 | 57 | public var scrollIndicatorHeight: CGFloat = 5 { 58 | didSet { 59 | scrollIndicatorConstraints?.height?.constant = scrollIndicatorHeight 60 | } 61 | } 62 | 63 | public let collectionView: UICollectionView = { 64 | let layout = UICollectionViewFlowLayout() 65 | layout.scrollDirection = .horizontal 66 | let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) 67 | collectionView.translatesAutoresizingMaskIntoConstraints = false 68 | collectionView.showsHorizontalScrollIndicator = false 69 | collectionView.backgroundColor = .clear 70 | return collectionView 71 | }() 72 | 73 | public let scrollIndicator: UIView = { 74 | let view = UIView() 75 | view.backgroundColor = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1) 76 | view.translatesAutoresizingMaskIntoConstraints = false 77 | return view 78 | }() 79 | 80 | public let bufferView: UIView = { 81 | let view = UIView() 82 | view.translatesAutoresizingMaskIntoConstraints = false 83 | return view 84 | }() 85 | 86 | public let blurView: UIVisualEffectView = { 87 | let blurEffect = UIBlurEffect(style: .light) 88 | let view = UIVisualEffectView(effect: blurEffect) 89 | view.translatesAutoresizingMaskIntoConstraints = false 90 | return view 91 | }() 92 | 93 | public var isTranslucent: Bool = false { 94 | didSet { 95 | if isTranslucent && blurView.superview == nil { 96 | insertSubview(blurView, at: 0) 97 | blurView.fillSuperview() 98 | } 99 | blurView.isHidden = !isTranslucent 100 | backgroundColor = isTranslucent ? (backgroundColor?.withAlphaComponent(0.75) ?? UIColor.white.withAlphaComponent(0.75)) : .white 101 | } 102 | } 103 | 104 | // MARK: - Properties [Private] 105 | 106 | public var scrollIndicatorWidth: CGFloat = 0 { 107 | didSet { 108 | scrollIndicatorConstraints?.width?.constant = scrollIndicatorWidth 109 | } 110 | } 111 | 112 | fileprivate var scrollIndicatorConstraints: NSLayoutConstraintSet? 113 | 114 | // MARK: - Initialization 115 | 116 | public override init(frame: CGRect) { 117 | super.init(frame: frame) 118 | setup() 119 | } 120 | 121 | required public init?(coder aDecoder: NSCoder) { 122 | super.init(coder: aDecoder) 123 | setup() 124 | } 125 | 126 | // MARK: - Setup [Private] 127 | 128 | fileprivate func setup() { 129 | backgroundColor = .white 130 | setupSubviews() 131 | setupConstraints() 132 | } 133 | 134 | fileprivate func setupSubviews() { 135 | addSubview(bufferView) 136 | addSubview(collectionView) 137 | collectionView.addSubview(scrollIndicator) 138 | } 139 | 140 | fileprivate func setupConstraints() { 141 | 142 | collectionView.addConstraints(topAnchor, left: leftAnchor, bottom: bufferView.topAnchor, right: rightAnchor) 143 | bufferView.addConstraints(collectionView.bottomAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor) 144 | 145 | switch scrollIndicatorPosition { 146 | case .top: 147 | 148 | scrollIndicatorConstraints = NSLayoutConstraintSet( 149 | top: scrollIndicator.topAnchor.constraint(equalTo: topAnchor), 150 | left: scrollIndicator.leadingAnchor.constraint(equalTo: collectionView.leadingAnchor), 151 | width: scrollIndicator.widthAnchor.constraint(equalToConstant: scrollIndicatorWidth), 152 | height: scrollIndicator.heightAnchor.constraint(equalToConstant: scrollIndicatorHeight) 153 | ).activate() 154 | 155 | case .bottom: 156 | 157 | scrollIndicatorConstraints = NSLayoutConstraintSet( 158 | bottom: scrollIndicator.bottomAnchor.constraint(equalTo: bottomAnchor), 159 | left: scrollIndicator.leadingAnchor.constraint(equalTo: collectionView.leadingAnchor), 160 | width: scrollIndicator.widthAnchor.constraint(equalToConstant: scrollIndicatorWidth), 161 | height: scrollIndicator.heightAnchor.constraint(equalToConstant: scrollIndicatorHeight) 162 | ).activate() 163 | 164 | } 165 | } 166 | 167 | // MARK: - Methods [Public] 168 | 169 | public func scrollCurrentBarView(from oldIndex: Int, to newIndex: Int, with offset: CGFloat) { 170 | 171 | deselectVisibleCells() 172 | let currentIndexPath = IndexPath(row: oldIndex, section: 0) 173 | let nextIndexPath = IndexPath(row: newIndex, section: 0) 174 | guard let currentCell = collectionView.cellForItem(at: currentIndexPath) as? DynamicTabBarCell, let nextCell = collectionView.cellForItem(at: nextIndexPath) as? DynamicTabBarCell else { return } 175 | let scrollRate = offset / frame.width 176 | let width = abs(scrollRate) * (nextCell.frame.width - currentCell.frame.width) 177 | let newOffset = currentCell.frame.minX + scrollRate * (scrollRate > 0 ? currentCell.frame.width : nextCell.frame.width) 178 | scrollIndicatorConstraints?.left?.constant = newOffset 179 | scrollIndicatorWidth = currentCell.frame.width + width 180 | collectionView.layoutIfNeeded() 181 | } 182 | 183 | public func moveCurrentBarView(to index: Int, animated: Bool, shouldScroll: Bool) { 184 | 185 | let indexPath = IndexPath(item: index, section: 0) 186 | if shouldScroll { 187 | collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: animated) 188 | layoutIfNeeded() 189 | } 190 | 191 | guard let cell = collectionView.cellForItem(at: indexPath) as? DynamicTabBarCell else { return } 192 | scrollIndicatorWidth = cell.frame.width 193 | scrollIndicatorConstraints?.left?.constant = cell.frame.origin.x 194 | if animated { 195 | UIView.animate(withDuration: 0.3, animations: { 196 | self.layoutIfNeeded() 197 | }, completion: { _ in 198 | self.updateUserInteraction(isEnabled: true) 199 | cell.isActive = true 200 | self.collectionView.reloadData() 201 | }) 202 | 203 | } else { 204 | layoutIfNeeded() 205 | cell.isActive = true 206 | collectionView.reloadData() 207 | updateUserInteraction(isEnabled: true) 208 | } 209 | } 210 | 211 | public func updateUserInteraction(isEnabled: Bool) { 212 | collectionView.isUserInteractionEnabled = isEnabled 213 | } 214 | 215 | // MARK: - Methods [Private] 216 | 217 | fileprivate func updateScrollIndicatorPosition() { 218 | scrollIndicatorConstraints?.deactivate() 219 | setupConstraints() 220 | } 221 | 222 | /// Updates the visible cells to their inactive state 223 | private func deselectVisibleCells() { 224 | collectionView.visibleCells.compactMap { $0 as? DynamicTabBarCell }.forEach { $0.tintColor = inactiveTintColor } 225 | } 226 | } 227 | -------------------------------------------------------------------------------- /DynamicTabBarController/DynamicTabBarCell.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DynamicTabBarCell.swift 3 | // DynamicTabBarController 4 | // 5 | // Copyright © 2017 Nathan Tannar. 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | // 25 | // Created by Nathan Tannar on 10/28/17. 26 | // 27 | 28 | import UIKit 29 | 30 | open class DynamicTabBarCell: UICollectionViewCell { 31 | 32 | // MARK: - Properties [Public] 33 | 34 | open class var reuseIdentifier: String { 35 | return "DynamicTabBarCell" 36 | } 37 | 38 | open var activeTintColor: UIColor? 39 | 40 | open var inactiveTintColor: UIColor? 41 | 42 | open var isActive: Bool = false { 43 | didSet { 44 | tintColor = isActive ? activeTintColor : inactiveTintColor 45 | } 46 | } 47 | 48 | // MARK: - Subviews [Public] 49 | 50 | open var label: UILabel = { 51 | let label = UILabel() 52 | label.textAlignment = .center 53 | label.font = UIFont.systemFont(ofSize: 12) 54 | label.adjustsFontSizeToFitWidth = true 55 | return label 56 | }() 57 | 58 | open var iconView: UIImageView = { 59 | let imageView = UIImageView() 60 | imageView.contentMode = .scaleAspectFit 61 | return imageView 62 | }() 63 | 64 | open var stackView: UIStackView = { 65 | let stackView = UIStackView() 66 | stackView.distribution = .fillEqually 67 | stackView.alignment = .center 68 | stackView.axis = .vertical 69 | return stackView 70 | }() 71 | 72 | // MARK: - Initialization 73 | 74 | override public init(frame: CGRect) { 75 | super.init(frame: frame) 76 | setup() 77 | } 78 | 79 | required public init?(coder aDecoder: NSCoder) { 80 | super.init(coder: aDecoder) 81 | setup() 82 | } 83 | 84 | // MARK: Setup [Private] 85 | 86 | fileprivate func setup() { 87 | stackView.addArrangedSubview(iconView) 88 | stackView.addArrangedSubview(label) 89 | contentView.addSubview(stackView) 90 | stackView.fillSuperview() 91 | } 92 | 93 | // MARK: - Methods [Public] 94 | 95 | open override func prepareForReuse() { 96 | super.prepareForReuse() 97 | label.text = nil 98 | iconView.image = nil 99 | } 100 | 101 | open override func tintColorDidChange() { 102 | super.tintColorDidChange() 103 | iconView.tintColor = tintColor 104 | label.textColor = tintColor 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /DynamicTabBarController/DynamicTabBarController.h: -------------------------------------------------------------------------------- 1 | // 2 | // DynamicTabBarController.h 3 | // DynamicTabBarController 4 | // 5 | // Created by Nathan Tannar on 11/14/17. 6 | // Copyright © 2017 Nathan Tannar. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for DynamicTabBarController. 12 | FOUNDATION_EXPORT double DynamicTabBarControllerVersionNumber; 13 | 14 | //! Project version string for DynamicTabBarController. 15 | FOUNDATION_EXPORT const unsigned char DynamicTabBarControllerVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /DynamicTabBarController/DynamicTabBarController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DynamicTabBarController.swift 3 | // DynamicTabBarController 4 | // 5 | // Copyright © 2017 Nathan Tannar. 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | // 25 | // Created by Nathan Tannar on 10/28/17. 26 | // 27 | 28 | import UIKit 29 | 30 | open class DynamicTabBarController: UIViewController { 31 | 32 | public enum TabBarSizeLayout { 33 | case fixed(CGFloat) 34 | case automatic 35 | } 36 | 37 | public enum TabBarPosition { 38 | case top, bottom 39 | } 40 | 41 | // MARK: - Properties [Public] 42 | 43 | public var currentIndex: Int { 44 | guard let currentViewController = pageViewController.viewControllers?.first else { return -1 } 45 | return viewControllers.index(of: currentViewController) ?? -1 46 | } 47 | 48 | open var isScrollEnabled: Bool { 49 | get { 50 | return pageScrollView?.isScrollEnabled ?? false 51 | } 52 | set { 53 | pageScrollView?.isScrollEnabled = newValue 54 | } 55 | } 56 | 57 | public fileprivate(set) var viewControllers: [UIViewController] 58 | 59 | public fileprivate(set) var pageScrollView: UIScrollView? 60 | 61 | public var tabBarSizeLayout: TabBarSizeLayout = .automatic { 62 | didSet { 63 | tabBar.collectionView.collectionViewLayout.invalidateLayout() 64 | tabBar.moveCurrentBarView(to: currentIndex, animated: false, shouldScroll: true) 65 | switch tabBarSizeLayout { 66 | case .fixed(let size): 67 | tabBar.scrollIndicatorWidth = size 68 | case .automatic: 69 | tabBar.scrollIndicatorWidth = tabBar.bounds.width / CGFloat(viewControllers.count) 70 | } 71 | } 72 | } 73 | 74 | public var tabBarPosition: TabBarPosition = .bottom { 75 | didSet { 76 | updateTabBarPosition() 77 | } 78 | } 79 | 80 | public fileprivate(set) var tabBarHeight: CGFloat = 60 81 | 82 | public var isTabBarTranslucent: Bool = false { 83 | didSet { 84 | tabBar.isTranslucent = isTabBarTranslucent 85 | updateTabBarPosition() 86 | } 87 | } 88 | 89 | // MARK: - Subviews [Public] 90 | 91 | public lazy var tabBar: DynamicTabBar = { [weak self] in 92 | let tabBar = DynamicTabBar() 93 | tabBar.translatesAutoresizingMaskIntoConstraints = false 94 | tabBar.backgroundColor = .white 95 | tabBar.collectionView.dataSource = self 96 | tabBar.collectionView.delegate = self 97 | tabBar.collectionView.register(DynamicTabBarCell.self, forCellWithReuseIdentifier: DynamicTabBarCell.reuseIdentifier) 98 | return tabBar 99 | }() 100 | 101 | public let trayView: UIView = { 102 | let view = UIView() 103 | view.translatesAutoresizingMaskIntoConstraints = false 104 | return view 105 | }() 106 | 107 | public lazy var pageViewController: UIPageViewController = { [weak self] in 108 | let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil) 109 | pageViewController.dataSource = self 110 | pageViewController.delegate = self 111 | pageViewController.view.translatesAutoresizingMaskIntoConstraints = false 112 | pageScrollView = pageViewController.view.subviews.compactMap { $0 as? UIScrollView }.first 113 | pageScrollView?.scrollsToTop = false 114 | pageScrollView?.delegate = self 115 | return pageViewController 116 | }() 117 | 118 | // MARK: - Properties [Private] 119 | 120 | fileprivate var shouldScrollIndicator = true 121 | fileprivate var previousIndex = 0 122 | 123 | fileprivate var tabBarConstraints: NSLayoutConstraintSet? 124 | fileprivate var pageViewControllerConstraints: NSLayoutConstraintSet? 125 | fileprivate var trayViewConstraints: NSLayoutConstraintSet? 126 | 127 | // MARK: - Initialization [Public] 128 | 129 | required public init(viewControllers: [UIViewController]) { 130 | self.viewControllers = viewControllers 131 | super.init(nibName: nil, bundle: nil) 132 | setup() 133 | } 134 | 135 | required public init?(coder aDecoder: NSCoder) { 136 | self.viewControllers = [UIViewController()] 137 | super.init(coder: aDecoder) 138 | setup() 139 | } 140 | 141 | // MARK: - Setup [Private] 142 | 143 | fileprivate func setup() { 144 | 145 | view.backgroundColor = .white 146 | setupSubviews() 147 | setupConstraints() 148 | setupPageViewController() 149 | } 150 | 151 | fileprivate func setupSubviews() { 152 | view.addSubview(pageViewController.view) 153 | view.addSubview(tabBar) 154 | view.addSubview(trayView) 155 | } 156 | 157 | fileprivate func setupConstraints() { 158 | 159 | switch tabBarPosition { 160 | case .top: 161 | 162 | let pageViewTop = isTabBarTranslucent ? view.topAnchor : tabBar.bottomAnchor 163 | pageViewControllerConstraints = NSLayoutConstraintSet( 164 | top: pageViewController.view.topAnchor.constraint(equalTo: pageViewTop), 165 | bottom: pageViewController.view.bottomAnchor.constraint(equalTo: trayView.topAnchor), 166 | left: pageViewController.view.leftAnchor.constraint(equalTo: view.leftAnchor), 167 | right: pageViewController.view.rightAnchor.constraint(equalTo: view.rightAnchor) 168 | ).activate() 169 | 170 | tabBarConstraints = NSLayoutConstraintSet( 171 | top: tabBar.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor), 172 | left: tabBar.leftAnchor.constraint(equalTo: view.leftAnchor), 173 | right: tabBar.rightAnchor.constraint(equalTo: view.rightAnchor), 174 | height: tabBar.heightAnchor.constraint(equalToConstant: tabBarHeight) 175 | ).activate() 176 | 177 | trayViewConstraints = NSLayoutConstraintSet( 178 | bottom: trayView.bottomAnchor.constraint(equalTo: view.bottomAnchor), 179 | left: trayView.leftAnchor.constraint(equalTo: view.leftAnchor), 180 | right: trayView.rightAnchor.constraint(equalTo: view.rightAnchor), 181 | height: trayView.heightAnchor.constraint(equalToConstant: 0) 182 | ).activate() 183 | 184 | case .bottom: 185 | 186 | let pageViewBottom = isTabBarTranslucent ? view.bottomAnchor : tabBar.topAnchor 187 | pageViewControllerConstraints = NSLayoutConstraintSet( 188 | top: pageViewController.view.topAnchor.constraint(equalTo: view.topAnchor), 189 | bottom: pageViewController.view.bottomAnchor.constraint(equalTo: pageViewBottom), 190 | left: pageViewController.view.leftAnchor.constraint(equalTo: view.leftAnchor), 191 | right: pageViewController.view.rightAnchor.constraint(equalTo: view.rightAnchor) 192 | ).activate() 193 | 194 | tabBarConstraints = NSLayoutConstraintSet( 195 | top: tabBar.bufferView.topAnchor.constraint(equalTo: trayView.topAnchor), 196 | bottom: tabBar.bufferView.bottomAnchor.constraint(equalTo: view.bottomAnchor), 197 | left: tabBar.leftAnchor.constraint(equalTo: view.leftAnchor), 198 | right: tabBar.rightAnchor.constraint(equalTo: view.rightAnchor), 199 | height: tabBar.collectionView.heightAnchor.constraint(equalToConstant: tabBarHeight) 200 | ).activate() 201 | 202 | trayViewConstraints = NSLayoutConstraintSet( 203 | bottom: trayView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor), 204 | left: trayView.leftAnchor.constraint(equalTo: view.leftAnchor), 205 | right: trayView.rightAnchor.constraint(equalTo: view.rightAnchor), 206 | height: trayView.heightAnchor.constraint(equalToConstant: 0) 207 | ).activate() 208 | 209 | } 210 | } 211 | 212 | fileprivate func setupPageViewController() { 213 | setupChildViewController(pageViewController) 214 | viewControllers.forEach { 215 | $0.loadViewIfNeeded() 216 | setupChildViewController($0) 217 | } 218 | pageViewController.setViewControllers([viewControllers[0]], direction: .forward, animated: false, completion: nil) 219 | pageScrollView?.isScrollEnabled = viewControllers.count > 1 220 | tabBar.moveCurrentBarView(to: currentIndex, animated: false, shouldScroll: true) 221 | } 222 | 223 | fileprivate func setupChildViewController(_ viewController: UIViewController) { 224 | viewController.willMove(toParent: self) 225 | addChild(pageViewController) 226 | viewController.didMove(toParent: self) 227 | } 228 | 229 | fileprivate func removeChildViewController(_ viewController: UIViewController) { 230 | viewController.willMove(toParent: nil) 231 | viewController.view.removeFromSuperview() 232 | viewController.removeFromParent() 233 | viewController.didMove(toParent: nil) 234 | } 235 | 236 | // MARK: - View Life Cycle [Public] 237 | 238 | open override func viewDidLoad() { 239 | super.viewDidLoad() 240 | 241 | } 242 | 243 | // MARK: - Tray View Methods [Public] 244 | 245 | open func toastAlert(text: String, font: UIFont = .systemFont(ofSize: 15), duration: TimeInterval? = nil, completion: ((Bool)->Void)? = nil) { 246 | let label = UILabel() 247 | label.font = font 248 | label.textAlignment = .center 249 | label.text = text 250 | label.textColor = .white 251 | let maxSize = CGSize(width: trayView.bounds.width, height: .greatestFiniteMagnitude) 252 | let heightToFit = label.sizeThatFits(maxSize).height.rounded() + 4 253 | trayView.subviews.forEach { $0.removeFromSuperview() } 254 | trayView.addSubview(label) 255 | label.fillSuperview() 256 | showTrayView(withHeight: heightToFit) { success in 257 | guard let duration = duration else { 258 | completion?(success) 259 | return 260 | } 261 | DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration, execute: { 262 | self.closeTrayView(completion: completion) 263 | }) 264 | } 265 | } 266 | 267 | open func showTrayView(withHeight height: CGFloat, withDuration duration: TimeInterval = 0.3, completion: ((Bool)->Void)? = nil) { 268 | let bottomConstant = UIScreen.main.nativeBounds.height == 2436 && tabBarPosition == .top ? view.layoutMargins.bottom : 0 269 | UIView.animate(withDuration: duration, animations: { 270 | self.trayViewConstraints?.height?.constant = height 271 | self.trayViewConstraints?.bottom?.constant = -bottomConstant 272 | self.view.layoutIfNeeded() 273 | }, completion: completion) 274 | } 275 | 276 | open func closeTrayView(withDuration duration: TimeInterval = 0.3, completion: ((Bool)->Void)? = nil) { 277 | UIView.animate(withDuration: duration, animations: { 278 | self.trayViewConstraints?.height?.constant = 0 279 | self.trayViewConstraints?.bottom?.constant = 0 280 | self.view.layoutIfNeeded() 281 | }, completion: completion) 282 | } 283 | 284 | // MARK: - Size Transition Methods [Public] 285 | 286 | public func updateTabBarHeight(to newValue: CGFloat, animated: Bool, duration: TimeInterval = 0.3) { 287 | let marginInset = isTabBarTranslucent ? view.layoutMargins.bottom : 0 288 | tabBarHeight = newValue + marginInset 289 | let adjustments = { 290 | self.tabBarConstraints?.height?.constant = self.tabBarHeight 291 | self.view.layoutIfNeeded() 292 | } 293 | if animated { UIView.animate(withDuration: duration, animations: adjustments) } 294 | else { adjustments() } 295 | tabBar.collectionView.collectionViewLayout.invalidateLayout() 296 | } 297 | 298 | open override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) { 299 | super.willTransition(to: newCollection, with: coordinator) 300 | DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.15) { 301 | self.tabBar.collectionView.collectionViewLayout.invalidateLayout() 302 | self.tabBar.setNeedsDisplay() 303 | self.tabBar.moveCurrentBarView(to: self.currentIndex, animated: false, shouldScroll: true) 304 | } 305 | } 306 | 307 | // MARK: - View Controller Array Methods [Public] 308 | 309 | public func addViewController(_ viewController: UIViewController, animated: Bool) { 310 | insertViewController(viewController, at: viewControllers.count, animated: animated) 311 | } 312 | 313 | public func insertViewController(_ viewController: UIViewController, at index: Int, animated: Bool) { 314 | guard index <= viewControllers.count else { return } 315 | viewControllers.insert(viewController, at: index) 316 | if animated { 317 | tabBar.collectionView.insertItems(at: [IndexPath(row: index, section: 0)]) 318 | } else { 319 | tabBar.collectionView.reloadData() 320 | } 321 | displayViewController(at: currentIndex, animated: animated) 322 | pageViewController.dataSource = nil 323 | pageViewController.dataSource = self 324 | } 325 | 326 | public func removeViewController(_ viewController: UIViewController, animated: Bool) { 327 | guard let index = viewControllers.index(of: viewController) else { return } 328 | removeViewController(at: index, animated: animated) 329 | } 330 | 331 | public func removeViewController(at index: Int, animated: Bool) { 332 | guard viewControllers.count > 1 else { return } 333 | if index == currentIndex { 334 | let adjustment = currentIndex == (viewControllers.count - 1) ? -1 : 1 335 | displayViewController(at: currentIndex + adjustment, animated: animated) 336 | } 337 | viewControllers.remove(at: index) 338 | if animated { 339 | self.pageViewController.view.isUserInteractionEnabled = false 340 | tabBar.collectionView.performBatchUpdates({ 341 | self.tabBar.collectionView.deleteItems(at: [IndexPath(row: index, section: 0)]) 342 | }, completion: { _ in 343 | self.pageViewController.view.isUserInteractionEnabled = true 344 | }) 345 | } else { 346 | self.tabBar.collectionView.deleteItems(at: [IndexPath(row: index, section: 0)]) 347 | } 348 | tabBar.moveCurrentBarView(to: currentIndex, animated: animated, shouldScroll: true) 349 | previousIndex = currentIndex 350 | } 351 | 352 | public func displayViewController(_ viewController: UIViewController, animated: Bool) { 353 | guard let index = viewControllers.index(of: viewController) else { return } 354 | displayViewController(at: index, animated: animated) 355 | } 356 | 357 | public func displayViewController(at index: Int, animated: Bool) { 358 | guard index < viewControllers.count else { return } 359 | previousIndex = index 360 | shouldScrollIndicator = false 361 | let direction: UIPageViewController.NavigationDirection = index > currentIndex ? .forward : .reverse 362 | pageViewController.setViewControllers([viewControllers[index]], direction: direction, animated: animated) { [weak self] _ in 363 | self?.shouldScrollIndicator = true 364 | } 365 | tabBar.moveCurrentBarView(to: currentIndex, animated: animated, shouldScroll: true) 366 | } 367 | 368 | // MARK: - Methods [Private] 369 | 370 | fileprivate func updateTabBarPosition() { 371 | pageViewControllerConstraints?.deactivate() 372 | tabBarConstraints?.deactivate() 373 | trayViewConstraints?.deactivate() 374 | setupConstraints() 375 | tabBar.collectionView.collectionViewLayout.invalidateLayout() 376 | } 377 | 378 | } 379 | 380 | // MARK: - UIPageViewControllerDataSource 381 | extension DynamicTabBarController: UIPageViewControllerDataSource { 382 | 383 | open func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { 384 | return nextViewController(viewController, isAfter: true) 385 | } 386 | 387 | open func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { 388 | return nextViewController(viewController, isAfter: false) 389 | } 390 | 391 | // MARK: - UIPageViewControllerDataSource Helper 392 | 393 | fileprivate func nextViewController(_ viewController: UIViewController, isAfter: Bool) -> UIViewController? { 394 | guard var index = viewControllers.index(of: viewController) else { return nil } 395 | index += isAfter ? 1 : -1 396 | guard index >= 0 && index < viewControllers.count else { return nil } 397 | return viewControllers[index] 398 | } 399 | 400 | } 401 | 402 | // MARK: - UIPageViewControllerDataSource 403 | extension DynamicTabBarController: UIPageViewControllerDelegate { 404 | 405 | open func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) { 406 | shouldScrollIndicator = true 407 | tabBar.updateUserInteraction(isEnabled: false) 408 | } 409 | 410 | open func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { 411 | if completed { 412 | tabBar.moveCurrentBarView(to: currentIndex, animated: true, shouldScroll: true) 413 | previousIndex = currentIndex 414 | } 415 | } 416 | 417 | } 418 | 419 | // MARK: - UICollectionViewDataSource 420 | extension DynamicTabBarController: UICollectionViewDataSource { 421 | 422 | final public func numberOfSections(in collectionView: UICollectionView) -> Int { 423 | return 1 424 | } 425 | 426 | final public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 427 | return viewControllers.count 428 | } 429 | 430 | open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 431 | 432 | guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DynamicTabBarCell.reuseIdentifier, for: indexPath) as? DynamicTabBarCell else { 433 | fatalError("DynamicTabBarCell has not been registered as a reusable cell") 434 | } 435 | let isCurrent = indexPath.row == (currentIndex % viewControllers.count) 436 | cell.activeTintColor = tabBar.activeTintColor 437 | cell.inactiveTintColor = tabBar.inactiveTintColor 438 | cell.isActive = isCurrent 439 | let image = isCurrent ? viewControllers[indexPath.row].tabBarItem.selectedImage : viewControllers[indexPath.row].tabBarItem.image 440 | cell.iconView.image = image 441 | cell.stackView.distribution = image != nil ? .fillEqually : .fillProportionally 442 | cell.label.text = viewControllers[indexPath.row].title 443 | return cell 444 | } 445 | 446 | } 447 | 448 | // MARK: - UICollectionViewDelegate 449 | extension DynamicTabBarController: UICollectionViewDelegate { 450 | 451 | open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 452 | 453 | defer { displayViewController(at: indexPath.row, animated: true) } 454 | 455 | tabBar.updateUserInteraction(isEnabled: false) 456 | tabBar.moveCurrentBarView(to: indexPath.row, animated: true, shouldScroll: true) 457 | 458 | // If the currentIndex is selected try to pop to a UINav root 459 | guard currentIndex == indexPath.row, let navigationController = viewControllers[indexPath.row] as? UINavigationController else { return } 460 | navigationController.popToRootViewController(animated: true) 461 | } 462 | 463 | } 464 | 465 | // MARK: - UICollectionViewDelegateFlowLayout 466 | extension DynamicTabBarController: UICollectionViewDelegateFlowLayout { 467 | 468 | open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 469 | var width: CGFloat 470 | switch tabBarSizeLayout { 471 | case .fixed(let val): 472 | width = val 473 | case .automatic: 474 | width = collectionView.frame.width / CGFloat(viewControllers.count) 475 | } 476 | let inset = self.collectionView(collectionView, layout: collectionViewLayout, insetForSectionAt: indexPath.section) 477 | switch tabBar.scrollIndicatorPosition { 478 | case .top: 479 | return CGSize(width: width, height: collectionView.frame.height - inset.top - inset.bottom) 480 | case .bottom: 481 | return CGSize(width: width, height: collectionView.frame.height - inset.top - inset.bottom) 482 | } 483 | } 484 | 485 | open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 486 | return 0 487 | } 488 | 489 | open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 490 | return 0 491 | } 492 | 493 | public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 494 | let constant: CGFloat = 4 495 | switch tabBar.scrollIndicatorPosition { 496 | case .top: 497 | return UIEdgeInsets(top: tabBar.scrollIndicatorHeight + constant, left: 0, bottom: constant, right: 0) 498 | case .bottom: 499 | return UIEdgeInsets(top: constant, left: 0, bottom: tabBar.scrollIndicatorHeight + constant, right: 0) 500 | } 501 | } 502 | } 503 | 504 | // MARK: - UIScrollViewDelegate 505 | extension DynamicTabBarController: UIScrollViewDelegate { 506 | 507 | open func scrollViewDidScroll(_ scrollView: UIScrollView) { 508 | 509 | if scrollView == pageScrollView { 510 | guard scrollView.contentOffset.x != view.bounds.width, shouldScrollIndicator else { return } 511 | let index = previousIndex + (scrollView.contentOffset.x > view.bounds.width ? 1 : -1) 512 | guard index >= 0, index < viewControllers.count else { return } 513 | let scrollOffsetX = scrollView.contentOffset.x - view.frame.width 514 | tabBar.scrollCurrentBarView(from: previousIndex, to: index, with: scrollOffsetX) 515 | } else if scrollView == tabBar.collectionView { 516 | switch tabBarSizeLayout { 517 | case .automatic: 518 | scrollView.contentOffset.x = 0 519 | case .fixed(_): 520 | break 521 | } 522 | } 523 | } 524 | 525 | } 526 | 527 | extension UIViewController { 528 | 529 | public var dynamicTabBarController: DynamicTabBarController? { 530 | var parentViewController = parent 531 | while parentViewController != nil { 532 | if let superViewController = parentViewController as? DynamicTabBarController { 533 | return superViewController 534 | } 535 | parentViewController = parentViewController!.parent 536 | } 537 | return nil 538 | } 539 | } 540 | -------------------------------------------------------------------------------- /DynamicTabBarController/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | NSPrincipalClass 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /DynamicTabBarController/NSConstraintLayoutSet.swift: -------------------------------------------------------------------------------- 1 | // 2 | // NSLayoutConstraintSet.swift 3 | // DynamicTabBarController 4 | // 5 | // Copyright © 2017 Nathan Tannar. 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | // 25 | // Created by Nathan Tannar on 10/28/17. 26 | // 27 | 28 | import UIKit 29 | 30 | class NSLayoutConstraintSet { 31 | 32 | var top: NSLayoutConstraint? 33 | var bottom: NSLayoutConstraint? 34 | var left: NSLayoutConstraint? 35 | var right: NSLayoutConstraint? 36 | var centerX: NSLayoutConstraint? 37 | var centerY: NSLayoutConstraint? 38 | var width: NSLayoutConstraint? 39 | var height: NSLayoutConstraint? 40 | 41 | public init(top: NSLayoutConstraint? = nil, bottom: NSLayoutConstraint? = nil, 42 | left: NSLayoutConstraint? = nil, right: NSLayoutConstraint? = nil, 43 | centerX: NSLayoutConstraint? = nil, centerY: NSLayoutConstraint? = nil, 44 | width: NSLayoutConstraint? = nil, height: NSLayoutConstraint? = nil) { 45 | self.top = top 46 | self.bottom = bottom 47 | self.left = left 48 | self.right = right 49 | self.centerX = centerX 50 | self.centerY = centerY 51 | self.width = width 52 | self.height = height 53 | } 54 | 55 | deinit { 56 | deactivate() 57 | } 58 | 59 | /// All of the currently configured constraints 60 | private var availableConstraints: [NSLayoutConstraint] { 61 | return [top, bottom, left, right, centerX, centerY, width, height].compactMap { $0 } 62 | } 63 | 64 | /// Activates all of the non-nil constraints 65 | /// 66 | /// - Returns: Self 67 | @discardableResult 68 | func activate() -> Self { 69 | NSLayoutConstraint.activate(availableConstraints) 70 | return self 71 | } 72 | 73 | /// Deactivates all of the non-nil constraints 74 | /// 75 | /// - Returns: Self 76 | @discardableResult 77 | func deactivate() -> Self { 78 | NSLayoutConstraint.deactivate(availableConstraints) 79 | return self 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /DynamicTabBarController/UIView+AutoLayout.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+AutoLayout.swift 3 | // DynamicTabBarController 4 | // 5 | // Copyright © 2017 Nathan Tannar. 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | // 25 | // Created by Nathan Tannar on 10/28/17. 26 | // 27 | 28 | import UIKit 29 | 30 | extension UIView { 31 | 32 | func fillSuperview() { 33 | guard let superview = self.superview else { return } 34 | translatesAutoresizingMaskIntoConstraints = false 35 | NSLayoutConstraint.activate([ 36 | leftAnchor.constraint(equalTo: superview.leftAnchor), 37 | rightAnchor.constraint(equalTo: superview.rightAnchor), 38 | topAnchor.constraint(equalTo: superview.topAnchor), 39 | bottomAnchor.constraint(equalTo: superview.bottomAnchor) 40 | ]) 41 | } 42 | 43 | @discardableResult 44 | func addConstraints(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, edgeInsets: UIEdgeInsets = .zero, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] { 45 | 46 | guard self.superview != nil else { return [] } 47 | translatesAutoresizingMaskIntoConstraints = false 48 | 49 | var constraints = [NSLayoutConstraint]() 50 | 51 | if let top = top { 52 | let constraint = topAnchor.constraint(equalTo: top, constant: edgeInsets.top) 53 | constraint.identifier = "top" 54 | constraints.append(constraint) 55 | } 56 | 57 | if let left = left { 58 | let constraint = leftAnchor.constraint(equalTo: left, constant: edgeInsets.left) 59 | constraint.identifier = "left" 60 | constraints.append(constraint) 61 | } 62 | 63 | if let bottom = bottom { 64 | let constraint = bottomAnchor.constraint(equalTo: bottom, constant: -edgeInsets.bottom) 65 | constraint.identifier = "bottom" 66 | constraints.append(constraint) 67 | } 68 | 69 | if let right = right { 70 | let constraint = rightAnchor.constraint(equalTo: right, constant: -edgeInsets.right) 71 | constraint.identifier = "right" 72 | constraints.append(constraint) 73 | } 74 | 75 | if widthConstant > 0 { 76 | let constraint = widthAnchor.constraint(equalToConstant: widthConstant) 77 | constraint.identifier = "width" 78 | constraints.append(constraint) 79 | } 80 | 81 | if heightConstant > 0 { 82 | let constraint = heightAnchor.constraint(equalToConstant: heightConstant) 83 | constraint.identifier = "height" 84 | constraints.append(constraint) 85 | } 86 | 87 | NSLayoutConstraint.activate(constraints) 88 | return constraints 89 | } 90 | 91 | func removeAllConstraints() { 92 | constraints.forEach { removeConstraint($0) } 93 | } 94 | 95 | @discardableResult 96 | func anchor(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, insets: UIEdgeInsets = .zero, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] { 97 | return anchor(top, left: left, bottom: bottom, right: right, topConstant: insets.top, leftConstant: insets.left, bottomConstant: insets.bottom, rightConstant: insets.right, widthConstant: widthConstant, heightConstant: heightConstant) 98 | } 99 | 100 | @discardableResult 101 | func anchor(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] { 102 | 103 | translatesAutoresizingMaskIntoConstraints = false 104 | var anchors = [NSLayoutConstraint]() 105 | 106 | if let top = top { 107 | let constraint = topAnchor.constraint(equalTo: top, constant: topConstant) 108 | constraint.identifier = "top" 109 | anchors.append(constraint) 110 | } 111 | 112 | if let left = left { 113 | let constraint = leftAnchor.constraint(equalTo: left, constant: leftConstant) 114 | constraint.identifier = "left" 115 | anchors.append(constraint) 116 | } 117 | 118 | if let bottom = bottom { 119 | let constraint = bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant) 120 | constraint.identifier = "bottom" 121 | anchors.append(constraint) 122 | } 123 | 124 | if let right = right { 125 | let constraint = rightAnchor.constraint(equalTo: right, constant: -rightConstant) 126 | constraint.identifier = "right" 127 | anchors.append(constraint) 128 | } 129 | 130 | if widthConstant > 0 { 131 | let constraint = widthAnchor.constraint(equalToConstant: widthConstant) 132 | constraint.identifier = "width" 133 | anchors.append(constraint) 134 | } 135 | 136 | if heightConstant > 0 { 137 | let constraint = heightAnchor.constraint(equalToConstant: heightConstant) 138 | constraint.identifier = "height" 139 | anchors.append(constraint) 140 | } 141 | 142 | NSLayoutConstraint.activate(anchors) 143 | return anchors 144 | } 145 | 146 | func anchorCenterXToSuperview(constant: CGFloat = 0) { 147 | 148 | translatesAutoresizingMaskIntoConstraints = false 149 | if let anchor = superview?.centerXAnchor { 150 | NSLayoutConstraint.activate([centerXAnchor.constraint(equalTo: anchor, constant: constant)]) 151 | } 152 | } 153 | 154 | func anchorCenterYToSuperview(constant: CGFloat = 0) { 155 | 156 | translatesAutoresizingMaskIntoConstraints = false 157 | if let anchor = superview?.centerYAnchor { 158 | NSLayoutConstraint.activate([centerYAnchor.constraint(equalTo: anchor, constant: constant)]) 159 | } 160 | } 161 | 162 | func anchorCenterSuperview() { 163 | anchorCenterXToSuperview() 164 | anchorCenterYToSuperview() 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /Example/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Example 4 | // 5 | // Created by Nathan Tannar on 11/14/17. 6 | // Copyright © 2017 Nathan Tannar. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import DynamicTabBarController 11 | 12 | @UIApplicationMain 13 | final class AppDelegate: UIResponder, UIApplicationDelegate { 14 | 15 | var window: UIWindow? 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 18 | 19 | let isTranslucent = false 20 | 21 | var vcs: [UIViewController] = [UINavigationController(rootViewController: TableViewController())] 22 | for fruit in Fruit.all() { 23 | let vc = ViewController() 24 | vc.tabBarItem.image = fruit.image()?.withRenderingMode(.alwaysTemplate) 25 | vc.title = fruit.rawValue.capitalized 26 | vc.view.backgroundColor = fruit.color() 27 | let nav = UINavigationController(rootViewController: vc) 28 | nav.navigationBar.isTranslucent = isTranslucent 29 | nav.navigationBar.prefersLargeTitles = true 30 | vcs.append(nav) 31 | } 32 | 33 | window = UIWindow(frame: UIScreen.main.bounds) 34 | let tabBarController = DynamicTabBarController(viewControllers: vcs) 35 | tabBarController.isTabBarTranslucent = isTranslucent 36 | // tabBarController.updateTabBarHeight(to: 20, animated: false) 37 | // tabBarController.tabBarSizeLayout = .fixed(100) 38 | // tabBarController.isScrollEnabled = false 39 | window?.rootViewController = tabBarController 40 | window?.makeKeyAndVisible() 41 | return true 42 | } 43 | 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Example/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /Example/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /Example/Assets.xcassets/apple.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "icons8-apple_filled.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Example/Assets.xcassets/apple.imageset/icons8-apple_filled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nathantannar4/DynamicTabBarController/cfd4415124c97db01b36c8acd47195c32785ba7d/Example/Assets.xcassets/apple.imageset/icons8-apple_filled.png -------------------------------------------------------------------------------- /Example/Assets.xcassets/banana.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "icons8-banana_filled.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Example/Assets.xcassets/banana.imageset/icons8-banana_filled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nathantannar4/DynamicTabBarController/cfd4415124c97db01b36c8acd47195c32785ba7d/Example/Assets.xcassets/banana.imageset/icons8-banana_filled.png -------------------------------------------------------------------------------- /Example/Assets.xcassets/grapes.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "icons8-grapes_filled.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Example/Assets.xcassets/grapes.imageset/icons8-grapes_filled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nathantannar4/DynamicTabBarController/cfd4415124c97db01b36c8acd47195c32785ba7d/Example/Assets.xcassets/grapes.imageset/icons8-grapes_filled.png -------------------------------------------------------------------------------- /Example/Assets.xcassets/pear.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "icons8-pear_filled.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Example/Assets.xcassets/pear.imageset/icons8-pear_filled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nathantannar4/DynamicTabBarController/cfd4415124c97db01b36c8acd47195c32785ba7d/Example/Assets.xcassets/pear.imageset/icons8-pear_filled.png -------------------------------------------------------------------------------- /Example/Assets.xcassets/plum.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "icons8-plum_filled.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Example/Assets.xcassets/plum.imageset/icons8-plum_filled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nathantannar4/DynamicTabBarController/cfd4415124c97db01b36c8acd47195c32785ba7d/Example/Assets.xcassets/plum.imageset/icons8-plum_filled.png -------------------------------------------------------------------------------- /Example/Assets.xcassets/pumpkin.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "icons8-pumpkin_filled.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Example/Assets.xcassets/pumpkin.imageset/icons8-pumpkin_filled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nathantannar4/DynamicTabBarController/cfd4415124c97db01b36c8acd47195c32785ba7d/Example/Assets.xcassets/pumpkin.imageset/icons8-pumpkin_filled.png -------------------------------------------------------------------------------- /Example/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Example/Fruit.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Fruit.swift 3 | // Example 4 | // 5 | // Created by Nathan Tannar on 11/14/17. 6 | // Copyright © 2017 Nathan Tannar. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | enum Fruit: String { 12 | case apple = "apple" 13 | case banana = "banana" 14 | case plum = "plum" 15 | case pumpkin = "pumpkin" 16 | case pear = "pear" 17 | case grapes = "grapes" 18 | 19 | func image() -> UIImage? { 20 | return UIImage(named: rawValue) 21 | } 22 | 23 | func color() -> UIColor { 24 | switch self { 25 | case .apple: 26 | return .red 27 | case .banana: 28 | return .yellow 29 | case .plum: 30 | return .blue 31 | case .pumpkin: 32 | return .orange 33 | case .pear: 34 | return .green 35 | case .grapes: 36 | return .purple 37 | } 38 | } 39 | 40 | static func all() -> [Fruit] { 41 | let fruits: [Fruit] = [.apple, .banana, .plum, .pumpkin, .grapes, .pear] 42 | return fruits 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Example/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIRequiredDeviceCapabilities 26 | 27 | armv7 28 | 29 | UISupportedInterfaceOrientations 30 | 31 | UIInterfaceOrientationPortrait 32 | UIInterfaceOrientationLandscapeLeft 33 | UIInterfaceOrientationLandscapeRight 34 | 35 | UISupportedInterfaceOrientations~ipad 36 | 37 | UIInterfaceOrientationPortrait 38 | UIInterfaceOrientationPortraitUpsideDown 39 | UIInterfaceOrientationLandscapeLeft 40 | UIInterfaceOrientationLandscapeRight 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Example/TableViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TableViewController.swift 3 | // Example 4 | // 5 | // Created by Nathan Tannar on 2/15/18. 6 | // Copyright © 2018 Nathan Tannar. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class TableViewController: UITableViewController { 12 | 13 | 14 | override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 15 | return 1 16 | } 17 | 18 | override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 19 | let cell = UITableViewCell() 20 | cell.textLabel?.text = "Push" 21 | cell.accessoryType = .disclosureIndicator 22 | return cell 23 | } 24 | 25 | override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 26 | tableView.deselectRow(at: indexPath, animated: true) 27 | navigationController?.pushViewController(TableViewController(), animated: true) 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Example/UIView+AutoLayout.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+AutoLayout.swift 3 | // DynamicTabBarController 4 | // 5 | // Copyright © 2017 Nathan Tannar. 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | // 25 | // Created by Nathan Tannar on 10/28/17. 26 | // 27 | 28 | import UIKit 29 | 30 | extension UIView { 31 | 32 | func fillSuperview() { 33 | guard let superview = self.superview else { return } 34 | translatesAutoresizingMaskIntoConstraints = false 35 | NSLayoutConstraint.activate([ 36 | leftAnchor.constraint(equalTo: superview.leftAnchor), 37 | rightAnchor.constraint(equalTo: superview.rightAnchor), 38 | topAnchor.constraint(equalTo: superview.topAnchor), 39 | bottomAnchor.constraint(equalTo: superview.bottomAnchor) 40 | ]) 41 | } 42 | 43 | @discardableResult 44 | func addConstraints(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, edgeInsets: UIEdgeInsets = .zero, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] { 45 | 46 | guard self.superview != nil else { return [] } 47 | translatesAutoresizingMaskIntoConstraints = false 48 | 49 | var constraints = [NSLayoutConstraint]() 50 | 51 | if let top = top { 52 | let constraint = topAnchor.constraint(equalTo: top, constant: edgeInsets.top) 53 | constraint.identifier = "top" 54 | constraints.append(constraint) 55 | } 56 | 57 | if let left = left { 58 | let constraint = leftAnchor.constraint(equalTo: left, constant: edgeInsets.left) 59 | constraint.identifier = "left" 60 | constraints.append(constraint) 61 | } 62 | 63 | if let bottom = bottom { 64 | let constraint = bottomAnchor.constraint(equalTo: bottom, constant: -edgeInsets.bottom) 65 | constraint.identifier = "bottom" 66 | constraints.append(constraint) 67 | } 68 | 69 | if let right = right { 70 | let constraint = rightAnchor.constraint(equalTo: right, constant: -edgeInsets.right) 71 | constraint.identifier = "right" 72 | constraints.append(constraint) 73 | } 74 | 75 | if widthConstant > 0 { 76 | let constraint = widthAnchor.constraint(equalToConstant: widthConstant) 77 | constraint.identifier = "width" 78 | constraints.append(constraint) 79 | } 80 | 81 | if heightConstant > 0 { 82 | let constraint = heightAnchor.constraint(equalToConstant: heightConstant) 83 | constraint.identifier = "height" 84 | constraints.append(constraint) 85 | } 86 | 87 | NSLayoutConstraint.activate(constraints) 88 | return constraints 89 | } 90 | 91 | func removeAllConstraints() { 92 | constraints.forEach { removeConstraint($0) } 93 | } 94 | 95 | @discardableResult 96 | func anchor(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, insets: UIEdgeInsets = .zero, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] { 97 | return anchor(top, left: left, bottom: bottom, right: right, topConstant: insets.top, leftConstant: insets.left, bottomConstant: insets.bottom, rightConstant: insets.right, widthConstant: widthConstant, heightConstant: heightConstant) 98 | } 99 | 100 | @discardableResult 101 | func anchor(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] { 102 | 103 | translatesAutoresizingMaskIntoConstraints = false 104 | var anchors = [NSLayoutConstraint]() 105 | 106 | if let top = top { 107 | let constraint = topAnchor.constraint(equalTo: top, constant: topConstant) 108 | constraint.identifier = "top" 109 | anchors.append(constraint) 110 | } 111 | 112 | if let left = left { 113 | let constraint = leftAnchor.constraint(equalTo: left, constant: leftConstant) 114 | constraint.identifier = "left" 115 | anchors.append(constraint) 116 | } 117 | 118 | if let bottom = bottom { 119 | let constraint = bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant) 120 | constraint.identifier = "bottom" 121 | anchors.append(constraint) 122 | } 123 | 124 | if let right = right { 125 | let constraint = rightAnchor.constraint(equalTo: right, constant: -rightConstant) 126 | constraint.identifier = "right" 127 | anchors.append(constraint) 128 | } 129 | 130 | if widthConstant > 0 { 131 | let constraint = widthAnchor.constraint(equalToConstant: widthConstant) 132 | constraint.identifier = "width" 133 | anchors.append(constraint) 134 | } 135 | 136 | if heightConstant > 0 { 137 | let constraint = heightAnchor.constraint(equalToConstant: heightConstant) 138 | constraint.identifier = "height" 139 | anchors.append(constraint) 140 | } 141 | 142 | NSLayoutConstraint.activate(anchors) 143 | return anchors 144 | } 145 | 146 | func anchorCenterXToSuperview(constant: CGFloat = 0) { 147 | 148 | translatesAutoresizingMaskIntoConstraints = false 149 | if let anchor = superview?.centerXAnchor { 150 | NSLayoutConstraint.activate([centerXAnchor.constraint(equalTo: anchor, constant: constant)]) 151 | } 152 | } 153 | 154 | func anchorCenterYToSuperview(constant: CGFloat = 0) { 155 | 156 | translatesAutoresizingMaskIntoConstraints = false 157 | if let anchor = superview?.centerYAnchor { 158 | NSLayoutConstraint.activate([centerYAnchor.constraint(equalTo: anchor, constant: constant)]) 159 | } 160 | } 161 | 162 | func anchorCenterSuperview() { 163 | anchorCenterXToSuperview() 164 | anchorCenterYToSuperview() 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /Example/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // Example 4 | // 5 | // Created by Nathan Tannar on 11/14/17. 6 | // Copyright © 2017 Nathan Tannar. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import DynamicTabBarController 11 | 12 | class ViewController: UIViewController { 13 | 14 | lazy var addButton: UIButton = { [unowned self] in 15 | let button = UIButton() 16 | button.setTitle("Add ViewController", for: .normal) 17 | button.addTarget(self, action: #selector(ViewController.addVC), for: .touchUpInside) 18 | button.backgroundColor = .black 19 | return button 20 | }() 21 | 22 | lazy var deleteButton: UIButton = { [unowned self] in 23 | let button = UIButton() 24 | button.setTitle("Delete ViewController", for: .normal) 25 | button.addTarget(self, action: #selector(ViewController.deleteVC), for: .touchUpInside) 26 | button.backgroundColor = .black 27 | return button 28 | }() 29 | 30 | lazy var toastButton: UIButton = { [unowned self] in 31 | let button = UIButton() 32 | button.setTitle("Toast Network Alert", for: .normal) 33 | button.addTarget(self, action: #selector(ViewController.toast), for: .touchUpInside) 34 | button.backgroundColor = .black 35 | return button 36 | }() 37 | 38 | @objc 39 | func addVC() { 40 | let newVC = ViewController() 41 | newVC.title = "New VC" 42 | newVC.view.backgroundColor = .black 43 | self.dynamicTabBarController?.addViewController(newVC, animated: true) 44 | } 45 | 46 | @objc 47 | func deleteVC() { 48 | guard let currentIndex = self.dynamicTabBarController?.currentIndex else { return } 49 | self.dynamicTabBarController?.removeViewController(at: currentIndex, animated: true) 50 | } 51 | 52 | @objc 53 | func toast() { 54 | self.dynamicTabBarController?.trayView.backgroundColor = .red 55 | self.dynamicTabBarController?.toastAlert(text: "You appear to be offline", font: UIFont.boldSystemFont(ofSize: 14), duration: 3, completion: nil) 56 | } 57 | 58 | override func viewDidLoad() { 59 | super.viewDidLoad() 60 | 61 | view.addSubview(addButton) 62 | view.addSubview(deleteButton) 63 | view.addSubview(toastButton) 64 | 65 | addButton.translatesAutoresizingMaskIntoConstraints = false 66 | addButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 67 | addButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true 68 | deleteButton.addConstraints(addButton.bottomAnchor, left: addButton.leftAnchor, bottom: nil, right: addButton.rightAnchor) 69 | toastButton.addConstraints(deleteButton.bottomAnchor, left: addButton.leftAnchor, bottom: nil, right: addButton.rightAnchor) 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) Nathan Tannar 2017 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DynamicTabBarController 2 | An improved UITabBarController that is dynamic and has a scroll indicator line 3 | 4 | ### Installation via CocoaPods 5 | 6 | ```ruby 7 | pod 'DynamicTabBarController' 8 | ``` 9 | 10 | ### Requirements 11 | 12 | iOS 9.0+ 13 | Swift 4 14 | XCode 9.0+ 15 | 16 | ## Demo 17 | 18 | 19 | 20 | ## Changelog 21 | 22 | - 1.0.0 23 | - Initial Release 24 | 25 | ## Author 26 |

27 | 28 |

29 | 30 | **Nathan Tannar** - [https://nathantannar.me](https://nathantannar.me) 31 | 32 | ## License 33 | 34 | Distributed under the MIT license. See ``LICENSE`` for more information. 35 | 36 | --------------------------------------------------------------------------------