一 创建Tabbar为根视图控制器
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
GDMainTabbarViewController *mainTVC = [[GDMainTabbarViewController alloc] init];
// 设置tabbar 透明
mainTVC.tabBar.translucent = NO;
self.window.rootViewController = mainTVC;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
二. 创建一个属性按钮// 这里是这页突出的按钮
@property(nonatomic,strong) UIButton *button;
三。设置创建系统的Tabbar// 1.添加第一个控制器
MainViewController *oneVC = [[MainViewController alloc]init];
[self setUpOneChildViewController:oneVC image:[UIImage imageNamed:@"tab_home_icon"] title:@"首页"];
// oneVC.view.backgroundColor = [UIColor redColor];
// 2.添加第2个控制器
ContactsViewController *twoVC = [[ContactsViewController alloc]init];
[self setUpOneChildViewController:twoVC image:[UIImage imageNamed:@"js"] title:@"通讯"];
// 3.添加中心按钮
CenterViewController *cenVC = [[CenterViewController alloc] init];
[self setUpOneChildViewController:cenVC image:[UIImage imageNamed:@"main"] title:@"中心"];
// 4.添加第3个控制器
FindViewController *threeVC = [[FindViewController alloc]init];
[self setUpOneChildViewController:threeVC image:[UIImage imageNamed:@"qw"] title:@"发现"];
// 5.添加第4个控制器
MineViewController *fourVC = [[MineViewController alloc]init];
[self setUpOneChildViewController:fourVC image:[UIImage imageNamed:@"user"] title:@"我的"];
四。用自定义按钮替代中心的tabbar,并设置其中心位置与Tabbar相同button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(pressChange:) forControlEvents:UIControlEventTouchUpInside];
// UIViewAutoresizingFlexibleRightMargin 自动调整view的宽度,保证左边距和右边距不变
// UIViewAutoresizingFlexibleLeftMargin 不会随父视图的改变而改变
// UIViewAutoresizingFlexibleBottomMargin自动调整view的高度,以保证上边距和下边距不变
// UIViewAutoresizingFlexibleTopMargin 自动调整view与父视图右边距,以保证左边距不变
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
// 设定button大小为适应图片
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setImage:buttonImage forState:UIControlStateNormal];
[button setImage:selectedImage forState:UIControlStateSelected];
// 这个比较恶心 去掉选中button时候的阴影
button.adjustsImageWhenHighlighted = NO;
/*
* 核心代码:设置button的center 和 tabBar的 center 做对齐操作, 同时做出相对的上浮
*/
CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
CGPoint center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[self.view addSubview:button];
放上github的地址,大家下载看看吧
https://github.com/daniel1214/OutshotTabbarDemo