【iPhone代码片段之八】如何自定义Tab Bar Button .

核心代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect frame = [[UIScreen mainScreen] bounds];
//NSLog(@"x=%.2f,y=%.2f,width=%.2f,height=%.2f",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
self.window = [[UIWindow alloc] initWithFrame:frame];
[[NSBundle mainBundle]loadNibNamed:@"TabBarController" owner:self options:nil];
//[self.window addSubview:tabBarController.view];
self.window.rootViewController = self.tabBarController;
//添加中间的TabBarButton
[self addCenternTabBarButton];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

return YES;
}


-(void)addCenternTabBarButton{
//NSLog(@"addCenternTabBarButton");
//创建一个自定义button
UIButton *centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
//初始化button的背景图片
UIImage *centerButtonImg = [UIImage imageNamed:@"centerTabBarItem.png"];
//设置button的frame
centerButton.frame = CGRectMake(0, 0, centerButtonImg.size.width, centerButtonImg.size.height);
//设置button的背景图片
[centerButton setBackgroundImage:centerButtonImg forState:UIControlStateNormal];
//设置button的action
[centerButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];

CGFloat imageHeight = centerButtonImg.size.height;
CGFloat barHeight = self.tabBarController.tabBar.frame.size.height;
//NSLog(@"imageHeight=%2.f,barHeight=%.2f",imageHeight,barHeight);
CGFloat delta = imageHeight-barHeight;
//NSLog(@"delta=%.2f",delta);

//设置centerButton的中心位置
if(delta<=0){//如果图片高度小于等于TabBar高度
centerButton.center = self.tabBarController.tabBar.center;
}else{//如果图片高度大于TabBar高度
CGPoint center = self.tabBarController.tabBar.center;
center.y = center.y - delta/2.0;
centerButton.center = center;
}
//将centerButton加入到tabBarController中
[self.tabBarController.view addSubview:centerButton];
}


效果图如下:
[img]
[img]http://dl.iteye.com/upload/attachment/0078/7480/fb01c512-4529-3d7c-8caa-431a6df282c5.png[/img]
[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值