iOS 实现Tabbarcontroller中间自定义样式 最简单的方法
先上图:
如果我们要实现中间按钮自定义样式,方法应该蛮多,这里介绍一种最简单的。
1、创建类继承:UITabBarController,如下代码都是写在该类的 .m文件里
2、定义最中间的自定义样式,写在viewDidLoad里
UIButton *btn = [[UIButton alloc] init]; btn.layer.cornerRadius = 30; btn.layer.masksToBounds = YES; btn.backgroundColor = [UIColor redColor]; btn.frame = CGRectMake(0, 0, 60, 60); CGPoint center = self.tabBar.center; center.y=center.y-15; btn.center = center; [self.view addSubview:btn]; [btn addTarget:self action:@selector(hhhCLick) forControlEvents:UIControlEventTouchUpInside];
这里解释一下:
我们获取当前tabbar的center点,然后自定义一个按钮,给予自己想要的样式,然后add上去。
这里要注意一点,这里用的自定义按钮,没有用系统tabbarItem,所以最后setViewController的时候要留出位置。
完整代码如下:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. HomeViewController *home = [[HomeViewController alloc] init]; UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:home]; home.title = @"相册"; home.navigationItem.title = @"相册"; home.tabBarItem.image = [[UIImage imageNamed:@"home"] imageWithRenderingMode:UIImageRenderingModeAutomatic]; home.tabBarItem.selectedImage = [[UIImage imageNamed:@"home_selected"] imageWithRenderingMode:UIImageRenderingModeAutomatic]; VideoViewController *pic = [[VideoViewController alloc] init]; UINavigationController *picNav = [[UINavigationController alloc] initWithRootViewController:pic]; pic.title = @"视频"; pic.navigationItem.title = @"视频"; pic.tabBarItem.image = [[UIImage imageNamed:@"picture"] imageWithRenderingMode:UIImageRenderingModeAutomatic]; pic.tabBarItem.selectedImage = [[UIImage imageNamed:@"picture_selected"] imageWithRenderingMode:UIImageRenderingModeAutomatic]; SeetingViewController *setting = [[SeetingViewController alloc] init]; UINavigationController *setNav = [[UINavigationController alloc] initWithRootViewController:setting]; setting.tabBarItem.title = @"设置"; setting.navigationItem.title = @"设置"; setting.tabBarItem.image = [[UIImage imageNamed:@"setting"] imageWithRenderingMode:UIImageRenderingModeAutomatic]; setting.tabBarItem.selectedImage = [[UIImage imageNamed:@"setting_selected"] imageWithRenderingMode:UIImageRenderingModeAutomatic]; SeetingViewController *setting222 = [[SeetingViewController alloc] init]; UINavigationController *setNav222 = [[UINavigationController alloc] initWithRootViewController:setting222]; setting222.tabBarItem.title = @"设置2"; setting222.navigationItem.title = @"设置2"; setting222.tabBarItem.image = [[UIImage imageNamed:@"setting"] imageWithRenderingMode:UIImageRenderingModeAutomatic]; setting222.tabBarItem.selectedImage = [[UIImage imageNamed:@"setting_selected"] imageWithRenderingMode:UIImageRenderingModeAutomatic]; NSMutableArray *arry = [[NSMutableArray alloc] init]; [arry addObject:homeNav]; [arry addObject:picNav]; [arry addObject:[UIViewController new]]; [arry addObject:setNav222]; [arry addObject:setNav]; [self setViewControllers:arry animated:YES]; //-----------这里是自定义样式按钮----------- UIButton *btn = [[UIButton alloc] init]; btn.layer.cornerRadius = 30; btn.layer.masksToBounds = YES; btn.backgroundColor = [UIColor redColor]; btn.frame = CGRectMake(0, 0, 60, 60); CGPoint center = self.tabBar.center; center.y=center.y-15; btn.center = center; [self.view addSubview:btn]; [btn addTarget:self action:@selector(hhhCLick) forControlEvents:UIControlEventTouchUpInside]; }
点击按钮事件,自己可以Modal到新控制器。
-(void)hhhCLick{ SeetingViewController *setting111 = [[SeetingViewController alloc] init]; UINavigationController *setNav111 = [[UINavigationController alloc] initWithRootViewController:setting111]; setting111.tabBarItem.title = @""; setting111.navigationItem.title = @"设置1"; [self presentViewController:setNav111 animated:YES completion:nil]; }