自定义Tabbar方法—
来源:互联网 发布:最优化导论中文版 编辑:程序博客网 时间:2024/04/26 04:07
方法一 覆盖式
原理:在Tabbar上放button,让button覆盖掉tabbaritem,再给button添加事件,需要注意的地方是button的坐标
代码如下:
头文件
#import <UIKit/UIKit.h>
@interface BaseTabBarController :UITabBarController
-(void) addCenterButtonWithImage:(UIImage*)buttonImage
highlightImage:(UIImage*)highlightImage;
- (void)addLeftButtonWithImage:(UIImage *)buttonImage
highlightImage:(UIImage *)highlightImage;
- (void)addRightButtonWithImage:(UIImage *)buttonImage
highlightImage:(UIImage *)hightlightImage;
@end
.m文件
#import "BaseTabBarController.h"
#define kBtnLeftTag 0
#define kBtnRightTag 2
#define kBtnCenterTag 1
constint width =100.0f;
constfloat centerBtnWidth =67.0f;
constfloat centerBtnHeight =66.0f;
constfloat btnWidth =127.0f;
constfloat btnHeight =49.0f;
@interface BaseTabBarController ()
@end
@implementation BaseTabBarController
- (void)viewDidLoad
{
[superviewDidLoad];
self.tabBar.backgroundColor = [UIColorbrownColor];
[self.tabBarsetBackgroundImage:[UIImageimageNamed:@"tabbarBackground.png"]];
}
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
UIButton* button = [UIButtonbuttonWithType:UIButtonTypeCustom];
button.tag =kBtnCenterTag;
button.autoresizingMask =UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin |UIViewAutoresizingFlexibleTopMargin;
button.frame =CGRectMake(0.0,0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImageforState:UIControlStateNormal];
[button setBackgroundImage:highlightImageforState:UIControlStateHighlighted];
[button addTarget:selfaction:@selector(didSelectedTabBaritem:)forControlEvents:UIControlEventTouchUpInside];
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.viewaddSubview:button];
}
- (void)addLeftButtonWithImage:(UIImage *)buttonImage
highlightImage:(UIImage *)highlightImage{
UIButton* button = [UIButtonbuttonWithType:UIButtonTypeCustom];
button.tag =kBtnLeftTag;
[button addTarget:selfaction:@selector(didSelectedTabBaritem:)forControlEvents:UIControlEventTouchUpInside];
button.autoresizingMask =UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin |UIViewAutoresizingFlexibleTopMargin;
float leftBtn_x =0.0f;
float leftBtn_y =self.tabBar.frame.origin.y;
button.frame =CGRectMake(leftBtn_x, leftBtn_y,btnWidth,btnHeight);
[button setBackgroundImage:buttonImageforState:UIControlStateNormal];
[button setBackgroundImage:highlightImageforState:UIControlStateHighlighted];
[self.viewaddSubview:button];
}
- (void)addRightButtonWithImage:(UIImage *)buttonImage
highlightImage:(UIImage *)highlightImage{
UIButton* button = [UIButtonbuttonWithType:UIButtonTypeCustom];
button.tag =kBtnRightTag;
[button addTarget:selfaction:@selector(didSelectedTabBaritem:)forControlEvents:UIControlEventTouchUpInside];
button.autoresizingMask =UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin |UIViewAutoresizingFlexibleTopMargin;
float leftBtn_x =self.tabBar.frame.size.width -btnWidth;
float leftBtn_y =self.tabBar.frame.origin.y;
button.frame =CGRectMake(leftBtn_x, leftBtn_y,btnWidth,btnHeight);
[button setBackgroundImage:buttonImageforState:UIControlStateNormal];
[button setBackgroundImage:highlightImageforState:UIControlStateHighlighted];
[self.viewaddSubview:button];
}
- (void)didSelectedTabBaritem:(UIButton *)btn{
self.selectedIndex = btn.tag;
}
适用于较少的Tabbaritem且中间突出的,效果图
- 自定义Tabbar方法—
- 自定义Tabbar方法二
- 自定义tabBar的一个方法
- 非自定义tabbar显示全图片方法
- 自定义TabBar
- 自定义TabBar
- 自定义Tabbar
- 自定义tabbar
- 自定义tabbar
- 自定义 TabBar
- 自定义TabBar
- 自定义tabBar
- 自定义TabBar
- 自定义TabBar
- 自定义Tabbar
- 自定义tabbar
- 自定义 TabBar
- 自定义TabBar
- java(20130731)反射、单例模式、内存里分四个区域、实例化过程、对象的销毁、命名规范、异常
- Android Intent详解
- UML用例图总结
- android 记事
- 2.23 Creating Scrollable Content with UIScrollView
- 自定义Tabbar方法—
- How to get UITableView from UITableViewCell?
- webservice CXF入门图解 附实例源码
- JavaScript 判斷變量類型的方法
- android中listview的一些样式设置
- 在windows下安装ruby1.9.2,rails和DevKit
- UML类图几种关系的总结
- C#自定义用户控件
- linux 下 nginx 日志分割