A TabBarController with top TabBar and slide content view.
To integrate SPSlideTabBarController into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '7.0'
pod 'SPSlideTabBarController', '~> 1.0.0'
Then, run the following command:
$ pod install
Minimum iOS Target: iOS 7.0
- SPSlideTabBarController
-
Initialize a SPSlideTabBarController
TableViewController *tableViewController = [[TableViewController alloc] init]; [tableViewController setTitle:@"table"]; CollectionViewController *collectionViewController = [[CollectionViewController alloc] initWithCollectionViewLayout:[UICollectionViewFlowLayout new]]; [collectionViewController setTitle:@"collection"]; ScrollViewController *scrollViewController = [[ScrollViewController alloc] init]; [scrollViewController setTitle:@"scroll"]; ViewController *viewController = [[ViewController alloc] init]; [viewController setTitle:@"general"]; SPSlideTabBarController *slideTabBarController = [[SPSlideTabBarController alloc] initWithViewController:@[tableViewController, collectionViewController, scrollViewController, viewController] initTabIndex:2];
-
Selected a tab with a SPSlideTabBarController
- (void)selectTabIndex:(NSUInteger)tabIndex animated:(BOOL)animated;
-
Add a UIViewController to a SPSlideTabBarController
/** * add a viewController to the slideTabBarController * * 为当前的 slideTabBarController 增加一个 viewController * * @discussion the viewController and the tab bar item will be added at the last index by default. * @discussion 待加入的 viewController 和 tab bar item 会被默认加到最后一个 */ - (void)addViewController:(nonnull UIViewController *)viewController; /** * add a viewController to the slideTabBarController at the index * * 为当前的 slideTabBarController 增加一个 viewController,添加到 index 的位置 */ - (void)addViewController:(nonnull UIViewController *)viewController atIndex:(NSUInteger)tabIndex;
- Custom a slide tab bar
a easy way to define a custom slide tab bar is to define a view which conforms to the protocol <SPSlideTabBarProtocol>
.
There are two defined custom slide tab bar already.
-
SPFixedSlideTabBar
/** * a custom slide tab bar whose tabs' width is fixed which is depend on the slide tab bar's width. * * 一个定制的 slide tab bar. 所有 tab 都是固定宽度的,具体宽度是多少是根据 tab bar 的宽度来均分计算的。 */ @interface SPFixedSlideTabBar : UIView <SPSlideTabBarProtocol> @end
-
SPSizingSlideTabBar
/** * a custom slide tab bar whose tabs' width is depend on the content size of the tab. * * 一个定制的 slide tab bar. 所有 tab 的宽度都是根据 tab 的内容来自适应的。 */ @interface SPSizingSlideTabBar : SPFixedSlideTabBar @end
- Style slide tab bar item
[[SPSlideTabBarItem appearance] setBarItemSelectedTextColor:[UIColor blueColor]];
Then, a custom slideTabBar can call [[SPAppearance appearanceForClass:[item class]] startForwarding:item];
for each tabBarItem to apply the style.
-
example code
In the method
- (void)resetTabBarItemViews
in theSPFixedSlideTabBar
, there are following code:[self.slideTabBarItems enumerateObjectsUsingBlock:^(SPSlideTabBarItem *item, NSUInteger index, BOOL *stop) { /** * start apply the style to the item */ [[SPAppearance appearanceForClass:[item class]] startForwarding:item]; NSUInteger tag = index + 1000; UIButton *button = (UIButton *)[self.scrollView viewWithTag:tag]; if (button == nil) { /** * initialize the button */ button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setTag:tag]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.scrollView addSubview:button]; } /** * style the button */ if (item.attibutedTitle) { [button setAttributedTitle:item.attibutedTitle forState:UIControlStateNormal]; } else { [button setTitle:item.title forState:UIControlStateNormal]; [button.titleLabel setFont:item.barItemTextFont]; [button setTitleColor:item.barItemTextColor forState:UIControlStateNormal]; [button setTitleColor:item.barItemSelectedTextColor forState:UIControlStateHighlighted]; [button setTitleColor:item.barItemSelectedTextColor forState:UIControlStateSelected]; [button setTitleColor:item.barItemSelectedTextColor forState:UIControlStateSelected | UIControlStateHighlighted]; } [button setSelected:(index == self.selectedTabIndex)]; }];
SPSlideTabBarController is released under the MIT license. See LICENSE for details.