标签:
NS_ASSUME_NONNULL_BEGIN @class UIView, UIImage, UINavigationController, UITabBarItem; @protocol UITabBarControllerDelegate; NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding> @property(nullable, nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers; // If the number of view controllers is greater than the number displayable by a tab bar, a "More" navigation controller will automatically be shown. // The "More" navigation controller will not be returned by -viewControllers, but it may be returned by -selectedViewController. - (void)setViewControllers:(NSArray<__kindof UIViewController *> * __nullable)viewControllers animated:(BOOL)animated; @property(nullable, nonatomic, assign) __kindof UIViewController *selectedViewController; // This may return the "More" navigation controller if it exists. @property(nonatomic) NSUInteger selectedIndex; @property(nonatomic, readonly) UINavigationController *moreNavigationController __TVOS_PROHIBITED; // Returns the "More" navigation controller, creating it if it does not already exist. @property(nullable, nonatomic, copy) NSArray<__kindof UIViewController *> *customizableViewControllers __TVOS_PROHIBITED; // If non-nil, then the "More" view will include an "Edit" button that displays customization UI for the specified controllers. By default, all view controllers are customizable. @property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0); // Provided for -[UIActionSheet showFromTabBar:]. Attempting to modify the contents of the tab bar directly will throw an exception. @property(nullable, nonatomic,weak) id<UITabBarControllerDelegate> delegate; @end @protocol UITabBarControllerDelegate <NSObject> @optional - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0); - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController; - (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; - (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed __TVOS_PROHIBITED; - (UIInterfaceOrientationMask)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; - (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; - (nullable id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController NS_AVAILABLE_IOS(7_0); - (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0); @end @interface UIViewController (UITabBarControllerItem) @property(null_resettable, nonatomic, strong) UITabBarItem *tabBarItem; // Automatically created lazily with the view controller‘s title if it‘s not set explicitly. @property(nullable, nonatomic, readonly, strong) UITabBarController *tabBarController; // If the view controller has a tab bar controller as its ancestor, return it. Returns nil otherwise. @end NS_ASSUME_NONNULL_END
定义tabBarController控制器
- (void)viewDidLoad { [super viewDidLoad]; self.tabBar.backgroundColor = [UIColor grayColor]; //床架控制器 self.V1 = [[ViewController alloc] init]; //将控制器放入导航控制器中 UINavigationController *navc= [[UINavigationController alloc] initWithRootViewController:self.V1]; //设置tabBarItem的标题 navc.tabBarItem.title = @"成长"; //设置tabBarItem的标题图片 navc.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"tab_growth"]]; // navc.tabBarItem.image = [self YZImageWithColor:[UIColor whiteColor] andSize:CGSizeMake(CGRectGetWidth(self.tabBar.bounds)/4, CGRectGetHeight(self.tabBar.bounds))]; //设置tabBarItem标题点击之前的颜色 [navc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateNormal]; //设置tabBarItem标题点击之后的颜色 [navc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateSelected]; self.V3 = [[TwoViewController alloc] init]; UINavigationController *navc1= [[UINavigationController alloc] initWithRootViewController:self.V3]; navc1.navigationBar.barTintColor = [UIColor blueColor]; navc1.tabBarItem.title = @"我"; navc1.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"tab_mine"]]; // navc1.tabBarItem.image = [self YZImageWithColor:[UIColor whiteColor] andSize:CGSizeMake(CGRectGetWidth(self.tabBar.bounds)/4, CGRectGetHeight(self.tabBar.bounds))]; [navc1.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateNormal]; [navc1.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateSelected]; //将控制器放入数组中 NSArray * array = [NSArray arrayWithObjects:navc, navc1, nil]; //将控制器 [self setViewControllers:array animated:YES]; //调用函数设置tabBar背景颜色 self.tabBar.barTintColor = [UIColor purpleColor]; //调用函数设置tabBar对应的每一个控制器点击之后的图标 self.tabBar.selectionIndicatorImage = [self YZImageWithColor:[UIColor orangeColor] andSize:CGSizeMake(CGRectGetWidth(self.tabBar.bounds)/4, CGRectGetHeight(self.tabBar.bounds))]; //重写UITabBarController代理 self.delegate = self; }
//设置图片的颜色和大小
- (UIImage *)YZImageWithColor:(UIColor *)color andSize:(CGSize)size{
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
上方代码我们能够得到想要的任何形式的固定的tabBarController但是呢我们有时候需求需要给同一个tabBar控制器的主题添加不同的功能,这时候使用普通的tabBarController已经不能够满足我们的需求了。这时候我们就用到了他的代理方法
//点击tabBarController之后调用的时间 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { UITabBarItem *tabBarItem = [[self.viewControllers objectAtIndex:0] tabBarItem]; if(tabBarController.selectedIndex == 0){ if ([tabBarItem.title isEqualToString:@"相机"]) { //调用相机相册 [self yzxShowCamera]; } tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"tab_ camera"]]; tabBarItem.title = @"相机"; }else{ tabBarItem.title = @"成长"; tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"tab_growth"]]; } }
我们使用以上方法是的我们能够定义我们想要的tabBarController了,当然我们亦可以自定义tabBarController但是这个往往会使得我们的界面更加复杂话。而且调用原声的往往更加简单
标签:
原文地址:http://www.cnblogs.com/YU411524/p/5404232.html