BATabBarController
Overview
The standard TabBarController is very limited in terms of animations when you make a selection. This cocoapod allows you to use one with a sleek animation with customizable properties!
Recent Versions
Language | Version |
---|---|
Swift 5 | 2.0.1 |
Swift 4 | 1.0.1 |
Obj C | 0.1.6 |
Requirements
- iOS 11+ iPhone, iPad and iPod Touch devices
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Getting Started
Installation
CocoaPods
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate BATabBarController into your Xcode project using CocoaPods, specify it in your Podfile
:
platform :ios, '11.0'
use_frameworks!
target '<Your Target Name>' do
pod 'BATabBarController', '~> 2.0.0'
end
Then, run the following command:
$ pod install
Carthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate BATabBarController into your Xcode project using Carthage, specify it in your Cartfile
:
github "antiguab/BATabBarController" ~> 2.0.0
Run carthage update
to build the framework and drag the built BaTabBarController.framework
into your Xcode project.
Manually
If you prefer not to use either of the aforementioned dependency managers, you can integrate BATabBarController into your project manually by downloading the source file from the releases page.
Usage
Default with No Text in Tab Bar
The example below create the default BATabBarController
tab bar (default properties) without text.
(Swift)
let vc1 = UIViewController()
let vc2 = UIViewController()
let vc3 = UIViewController()
let tabBarItem = BATabBarItem(image: UIImage(named: "icon1_unselected")!, selectedImage: UIImage(named: "icon1_selected")!)
let tabBarItem2 = BATabBarItem(image: UIImage(named: "icon2_unselected")!, selectedImage: UIImage(named: "icon2_selected")!)
let tabBarItem3 = BATabBarItem(image: UIImage(named: "icon3_unselected")!, selectedImage: UIImage(named: "icon3_selected")!)
let baTabBarController = BATabBarController()
baTabBarController.viewControllers = [vc1, vc2, vc3]
baTabBarController.tabBarItems = [tabBarItem, tabBarItem2, tabBarItem3]
baTabBarController.delegate = self
self.view.addSubview(baTabBarController.view)
(Objective C)
UIViewController *vc1 = [[UIViewController alloc] init];
UIViewController *vc2 = [[UIViewController alloc] init];
UIViewController *vc3 = [[UIViewController alloc] init];
BATabBarItem *tabBarItem = [[BATabBarItem alloc] initWithImage:[UIImage imageNamed:@"icon1_unselected"]
selectedImage:[UIImage imageNamed:@"icon1_selected"]];
BATabBarItem *tabBarItem2 = [[BATabBarItem alloc] initWithImage:[UIImage imageNamed:@"icon2_unselected"]
selectedImage:[UIImage imageNamed:@"icon2_selected"]];
BATabBarItem *tabBarItem3 = [[BATabBarItem alloc] initWithImage:[UIImage imageNamed:@"icon3_unselected"]
selectedImage:[UIImage imageNamed:@"icon1_selected"]];
BATabBarController *baTabBarController = [[BATabBarController alloc] init];
baTabBarController.viewControllers = @[vc1,vc2,vc3];
baTabBarController.tabBarItems = @[tabBarItem,tabBarItem2,tabBarItem3]; baTabBarController.delegate = self;
[self.view addSubview:baTabBarController.view];
This creates the following:
Default with Text in Tab Bar
The example below create the default BATabBarController
tab bar (default properties) with text.
(Swift)
let vc1 = UIViewController()
let vc2 = UIViewController()
let vc3 = UIViewController()
let option1 = NSMutableAttributedString(string: "Feed")
option1.addAttribute(.foregroundColor, value: UIColor.white, range: NSRange(location: 0, length: option1.length))
tabBarItem = BATabBarItem(image: UIImage(named: "icon1_unselected")!, selectedImage: UIImage(named: "icon1_selected")!, title: option1)
tabBarItem2 = BATabBarItem(image: UIImage(named: "icon2_unselected")!, selectedImage: UIImage(named: "icon2_selected")!, title: option1)
tabBarItem3 = BATabBarItem(image: UIImage(named: "icon3_unselected")!, selectedImage: UIImage(named: "icon3_selected")!, title: option1)
let baTabBarController = BATabBarController()
baTabBarController.viewControllers = [vc1, vc2, vc3]
baTabBarController.tabBarItems = [tabBarItem, tabBarItem2, tabBarItem3]
baTabBarController.delegate = self
self.view.addSubview(baTabBarController.view)
(Objective C)
UIViewController *vc1 = [[UIViewController alloc] init];
UIViewController *vc2 = [[UIViewController alloc] init];
UIViewController *vc3 = [[UIViewController alloc] init];
NSMutableAttributedString *option1 = [[NSMutableAttributedString alloc] initWithString:@"Option1"];
[option1 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xF0F2F6] range:NSMakeRange(0,option1.length)];
BATabBarItem *tabBarItem = [[BATabBarItem alloc] initWithImage:[UIImage imageNamed:@"icon1_unselected"]
selectedImage:[UIImage imageNamed:@"icon1_selected"]
title:option1];
NSMutableAttributedString *option2 = [[NSMutableAttributedString alloc] initWithString:@"Option2"];
[option2 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xF0F2F6] range:NSMakeRange(0,option2.length)];
BATabBarItem *tabBarItem2 = [[BATabBarItem alloc] initWithImage:[UIImage imageNamed:@"icon2_unselected"]
selectedImage:[UIImage imageNamed:@"icon2_selected"]
title:option2];
NSMutableAttributedString * option3 = [[NSMutableAttributedString alloc] initWithString:@"Option3"];
[option3 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xF0F2F6] range:NSMakeRange(0,option3.length)];
BATabBarItem *tabBarItem3 = [[BATabBarItem alloc] initWithImage:[UIImage imageNamed:@"icon3_unselected"]
selectedImage:[UIImage imageNamed:@"icon3_selected"]
title:option3];
BATabBarController *baTabBarController = [[BATabBarController alloc] init];
baTabBarController.viewControllers = @[vc1,vc2,vc3];
baTabBarController.tabBarItems = @[tabBarItem,tabBarItem2,tabBarItem3]; baTabBarController.delegate = self;
[self.view addSubview:baTabBarController.view];
This creates the following:
Badges
Badges can be created with the BATabBarBadge
class. After creating a badge you can simply assign it to the .badge
property of a BATabBarItem
:
(Swift)
let badge = BATabBarBadge(value:8, badgeColor: .red)
tabBarItem2.badge = badge
let badge3 = BATabBarBadge(value:8234, badgeColor: .red)
tabBarItem3.badge = badge
(Objective C)
BATabBarBadge *badge = [[BATabBarBadge alloc] initWithValue:@8 backgroundColor:[UIColor redColor]];
tabBarItem.badge = badge;
BATabBarBadge *badge3 = [[BATabBarBadge alloc] initWithValue:@8234 backgroundColor:[UIColor redColor]];
tabBarItem3.badge = badge3;
This creates the following:
Methods
Set Initial View Controller (SWIFT ONLY)
You can set the initial selected controller programmatically by using assigning like below:
(Swift)
baTabBarController.initialViewController = vc2
Set Selected View Controller
You can change the currently selected tab bar item programmatically by using the method below. To avoid
the animation pass false
in the second parameter:
(Swift)
setSelectedViewController(_:animated:)
(Objective C)
setSelectedViewController:(UIViewController*)viewController animated:(BOOL)animated
BATabBarControllerDelegate
If you’d like to add external actions when a tab item is selected, you can use:
(Swift)
tabBarController(_:didSelect:)
(Objective C)
tabBarController:(BATabBarController *)tabBarControllerdidSelectViewController:(UIViewController *)viewController
Properties
Tab Bar Color
To change the color of the tab bar, you can change the tabBarBackgroundColor
property of the BATabBarController
instance.
(Swift)
baTabBarController.tabBarBackgroundColor = .black
(Objective C)
baTabBarController.tabBarBackgroundColor = [UIColor blackColor];
result:
Tab Bar Item Stroke Color
To change the color of the stroke in the animation, you can change the tabBarItemStrokeColor
property of the BATabBarController
instance.
(Swift)
baTabBarController.tabBarItemStrokeColor = .blue
(Objective C)
baTabBarController.tabBarItemStrokeColor = [UIColor blueColor];
result:
Tab Bar Item Line Width
To change the size of the stroke in the animation, you can change the tabBarItemLineWidth
property of the BATabBarController
instance.
(Swift)
baTabBarController.tabBarItemLineWidth = 1.0
(Objective C)
baTabBarController.tabBarItemLineWidth = 1.0;
result:
Hiding Tab Bar
The tab bar can be hidden via two properties.
(Swift)
baTabBarController.hidesBottomBarWhenPushed = true
(Objective C)
baTabBarController.hidesBottomBarWhenPushed = YES;
or
baTabBarController.tabBar.hidden = YES;
Tab Bar Animation Duration
To change the duration of the tab bar animation, you can change the tabBatAnimationDuration
property of the BATabBarController
instance.
(Swift)
baTabBarController.tabBarAnimationDuration = 1
(Objective C)
baTabBarController.tabBarAnimationDuration = 1;
Swift ChangeLog
Version 2.0.2 (4.11.2020)
- Fixed height constraint issues around bottom safe area insets
Version 2.0.1 (4.5.2020)
- Removed some unused properties
- Fixed issue where you can touchdown on multiple tabs causing animation issues
Version 2.0.0 (12.9.2019)
- Now available in Swift 5!
Version 1.0.1 (9.5.2019)
- Removed some schemas that didn’t need to be public for better Carthage builds
Version 1.0.0 (9.2.2019)
- Now available in Swift 4!
Objective-C ChangeLog
Version 0.1.6 (9.3.2019)
- Fixed release build bug where animation stopped showing up
Version 0.1.5 (12.21.2017)
- Added new
animationDuration
property (thanks to @oalsing) - Fixed bug where there were broken animations in release builds
- Updated image assets in the example project
- Updated toolbar height when on iPhone X
- bug fix where outline animation was not clearing when changing tab programmatically
Version 0.1.4 (1.11.2017)
- Added ability to show and hide the tab bar through the
hidden
property - Added ability to show and hide the tab bar through the
hidesBottomBarWhenPushed
property
Version 0.1.3 (10.23.2016)
- Added ability to have badge icons (thanks to @terhechte)
Version 0.1.2 (06.21.2016)
- Platform version supports 8.0+
Version 0.1.1 (05.20.2016)
- Platform version supports 8.1+
Version 0.1.0 (05.18.2016)
- Initial release
Further informations
- N/A
Known issues
Author
Bryan Antigua, antigua.B@gmail.com - bryanantigua.com
License
BATabBarController
is available under the MIT license. See the LICENSE file for more info.