UITabBarController自定义一
2016-02-16 16:13
501 查看
UITabBarController自定义一
首先在Appdelegate.m文件中将UITabBarController的子类设置为rootViewController,并设置其viewControllers1.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2. // Override point for customization after application launch. 3. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 4. self.window.backgroundColor = [UIColor whiteColor]; 5. [self.window makeKeyAndVisible]; 6. 7. JKBaseTabBarController *baseTabBarController = [self createRootController]; 8. 9. self.window.rootViewController = baseTabBarController; 10. 11. 12. return YES; 13.} 14. 15.#pragma mark - 创建跟视图 16.-(JKBaseTabBarController *)createRootController{ 17. JKBaseTabBarController *baseTabBarController = [[JKBaseTabBarController alloc] init]; 18. 19. // items 20.// NSArray *titles = @[@"总览",@"账户",@"图表"]; 21. 22. JKGeneralViewController *general = [[JKGeneralViewController alloc] init]; 23. general.tabBarItem.title = @"总览"; 24. general.tabBarItem.image = [[UIImage imageNamed:@"overview_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 25. general.tabBarItem.selectedImage = [[UIImage imageNamed:@"overview_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 26. 27. JKAccountViewController *account = [[JKAccountViewController alloc] init]; 28. account.tabBarItem.title = @"账户"; 29. account.tabBarItem.image = [[UIImage imageNamed:@"account_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 30. account.tabBarItem.selectedImage = [[UIImage imageNamed:@"account_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 31. 32. JKGraphViewController *graph = [[JKGraphViewController alloc] init]; 33. graph.tabBarItem.title = @"图表"; 34. graph.tabBarItem.image = [[UIImage imageNamed:@"graph_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 35. graph.tabBarItem.selectedImage = [[UIImage imageNamed:@"graph_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 36. 37. baseTabBarController.viewControllers = @[general,account,graph]; 38. 39. return baseTabBarController; 40.} 41. 42.
然后在UITabBarController子类的viewWillAppear方法中将tabBar的子视图隐藏掉,换上自定义的视图
1.#pragma mark - 自定义 2.-(void)viewWillAppear:(BOOL)animated{ 3. [super viewWillAppear:animated]; 4. self.tabBar.barTintColor = [UIColor blackColor]; 5. 6. // 先将系统自带的隐藏 7. for (UIView *view in self.tabBar.subviews) { 8. view.hidden = YES; 9. } 10. 11. NSArray *controllers = self.viewControllers; 12. 13. // 根据有多少项计算宽度 14. CGFloat h = self.tabBar.bounds.size.height; 15. CGFloat w = h; 16. CGFloat interval = (s_w - controllers.count * h) / (controllers.count + 1); 17. 18. for (int i = 0; i < controllers.count; i++) { 19. UITabBarItem *item = ((UIViewController *)controllers[i]).tabBarItem; 20. UIButton *btn = [[UIButton alloc] initWithFrame:(CGRect){interval + i * (w + interval),0,w,h}]; 21. 22. btn.imageEdgeInsets = UIEdgeInsetsMake(-15, 0, 0, 0); 23. 24. [btn setImage:item.image forState:UIControlStateNormal]; 25. [btn setImage:item.selectedImage forState:UIControlStateSelected]; 26. 27. btn.titleEdgeInsets = UIEdgeInsetsMake(30, -35, 0, 0); 28. btn.titleLabel.font = [UIFont systemFontOfSize:14.0f]; 29. [btn setTitle:item.title forState:UIControlStateNormal]; 30. [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 31. [btn setTitleColor:lightBlueColor forState:UIControlStateSelected]; 32. 33. 34. btn.tag = i + 200; 35. [btn addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside]; 36. 37. 38. if (i == 0) { 39. btn.selected = YES; 40. } 41. 42. [self.tabBar addSubview:btn]; 43. } 44.} 45. 46.-(void)changeViewController:(UIButton *)sender{ 47. if (!sender.selected) { 48. sender.selected = !sender.selected; 49. self.selectedIndex = sender.tag - 200; 50. 51. for (UIView *view in self.tabBar.subviews) { 52. if ([view isKindOfClass:[UIButton class]] && view.tag != sender.tag) { 53. UIButton *btn = (UIButton *)view; 54. btn.selected = NO; 55. } 56. } 57. } 58. 59.} 60.
效果如图
*没有上诉自定义的话也可以通过
[general.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} forState:UIControlStateNormal];方法改变title的颜色
相关文章推荐
- NGUI制作字体的三种方法
- Kbuild
- iPads和iPhones的Media Queries
- UICollectionView使用以及与UITableView的区别
- JAVA 生成uuid
- IOS学习 UIView 页面布局
- SOUI GDI+渲染引擎下的字体特效
- iOS对UIView对象或子类对象截屏(截取的内容以UIImage的形式存储)
- UICollectionView基础
- 降低对象的引用级别到软引用 class SoftValueMap<K, V> extends HashMap<K, V>
- nyoj--1100--WAJUEJI which home strong!(bfs)
- nyoj--1100--WAJUEJI which home strong!(bfs)
- android 开源组件合集-UI篇(2014-08-25更新)
- MySQL关于Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
- UITextView控件的用法
- easyUI datagird实现对行的任意拖拽移动位置
- MySQL的错误:No query specified
- 各种UIGestureRecognizer 手势
- UIWindow
- Android-节日短信送祝福(UI篇:3-选择短信与发送短信的Activity的实现)