UItabBar设置的一些设置 合一些效果实现 设置badgeValue的方法封装
2013-06-09 14:28
507 查看
ios默认的样式不太适合我们的应用,可以通过一下方法设置相关属性:
设置背景:
[_tabBar setBackgroundImage:[UIImage imageNamed:@"bg_tabbar"]];
设置某个Item选中的效果:
_tabBar.selectionIndicatorImage = [UIImage imageNamed:@"bar_item_selected"];//设置选中效果图片
设置UITabBarItem文字颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{ UITextAttributeTextColor
: [UIColor whiteColor] }
forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ UITextAttributeTextColor
: [UIColor whiteColor] }
forState:UIControlStateHighlighted];
设置UITabBarItem未选中与选中时的图片:
[_hotTabItem setFinishedSelectedImage:[UIImage imageNamed:@"1_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"1"]];
[_searchTabItem setFinishedSelectedImage:[UIImage imageNamed:@"2_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"2"]];
[_userTabItem setFinishedSelectedImage:[UIImage imageNamed:@"3_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"3"]];
上述方法基本可以满足了。
如果还不行可以自定义实现UITabBar。
bottomBar = [[UITabBaralloc]
initWithFrame:CGRectMake(0,CGRectGetMaxY(sframe)-25,self.view.frame.size.width,0)];//初始隐藏bottomBar
//[bottomBar setDelegate:self];
[self.viewaddSubview:bottomBar];
//按钮
CGFloat btnWidth = (sframe.size.width-165)/2;//两个按钮
CGFloat btnTop =0;//sframe.size.height-barHeight;
CGFloat _barHeight =
dHeightBar;
MU_tabButton* btn1 = [[MU_tabButtonalloc]
initWithFrame:CGRectMake(0, btnTop, btnWidth, _barHeight)];
[btn1 addTarget:selfaction:@selector(selectAllClicked:)forControlEvents:UIControlEventTouchUpInside];
[btn1 setBgImg:dImgTabBgandIconImg:dImgSelectAllandTitle:@"全选"withColor:dColorTabTextNormalforState:UIControlStateNormal];
MU_tabButton* btn2 = [[MU_tabButtonalloc]
initWithFrame:CGRectMake(btnWidth, btnTop, btnWidth, _barHeight)];
[btn2 setBackgroundImage:dImgTabClickedforState:UIControlStateHighlighted];
[btn2 addTarget:selfaction:@selector(deleteClicked:)forControlEvents:UIControlEventTouchUpInside];
[btn2 setBgImg:dImgTabBgandIconImg:dImgDeleteandTitle:@"删除"withColor:dColorTabTextNormalforState:UIControlStateNormal];
[bottomBaraddSubview:btn1];
[bottomBaraddSubview:btn2];
///////////其中MU_tabButton 为自定义类型
主要属性包含如下:
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
// Initialization code
CGFloat iconHeight = frame.size.height*3/5;
//文字
titleLabel = [[UILabelalloc]
initWithFrame:CGRectMake(0, iconHeight, frame.size.width, frame.size.height-
iconHeight)];
titleLabel.backgroundColor = [UIColorclearColor];
titleLabel.textColor = [UIColorwhiteColor];
titleLabel.textAlignment =NSTextAlignmentCenter;
titleLabel.font = [UIFontsystemFontOfSize:13];
[selfaddSubview:titleLabel];
//图标
CGFloat iconGap =
3;
CGFloat iconSize = iconHeight - iconGap;//图标大小
iconView = [[UIImageViewalloc]
initWithFrame:CGRectMake((frame.size.width- iconSize)/2, iconGap, iconSize, iconSize)];
[selfaddSubview:iconView];
}
return
self;
}
-(void)setBgImg:(UIImage *)bgImg andIconImg:(UIImage *)img andTitle:(NSString
*)title withColor:(UIColor*)tColor forState:(UIControlState)state
{
[selfsetBackgroundImage:bgImg
forState:UIControlStateNormal];
iconView.image = img;
titleLabel.text = title;
titleLabel.textColor = tColor;
}
-(void)setBadgeValue:(NSString*)val atTabIndex:(int)index
{
// UITabBarItem* tab = [[bottomBtnBar items] objectAtIndex:2*index];
UITabBarItem* tab = [[bottomBtnBar
items] objectAtIndex:index];
if ([val integerValue] <=
0) {
tab.badgeValue =
nil;
}
else
{
tab.badgeValue = val;
}
}
-(NSString*)getBadgeValueAtIndex:(int)index
{
UITabBarItem* tab = [[bottomBtnBar
items] objectAtIndex:index];
return tab.badgeValue;
}
设置背景:
[_tabBar setBackgroundImage:[UIImage imageNamed:@"bg_tabbar"]];
设置某个Item选中的效果:
_tabBar.selectionIndicatorImage = [UIImage imageNamed:@"bar_item_selected"];//设置选中效果图片
设置UITabBarItem文字颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{ UITextAttributeTextColor
: [UIColor whiteColor] }
forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ UITextAttributeTextColor
: [UIColor whiteColor] }
forState:UIControlStateHighlighted];
设置UITabBarItem未选中与选中时的图片:
[_hotTabItem setFinishedSelectedImage:[UIImage imageNamed:@"1_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"1"]];
[_searchTabItem setFinishedSelectedImage:[UIImage imageNamed:@"2_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"2"]];
[_userTabItem setFinishedSelectedImage:[UIImage imageNamed:@"3_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"3"]];
上述方法基本可以满足了。
如果还不行可以自定义实现UITabBar。
bottomBar = [[UITabBaralloc]
initWithFrame:CGRectMake(0,CGRectGetMaxY(sframe)-25,self.view.frame.size.width,0)];//初始隐藏bottomBar
//[bottomBar setDelegate:self];
[self.viewaddSubview:bottomBar];
//按钮
CGFloat btnWidth = (sframe.size.width-165)/2;//两个按钮
CGFloat btnTop =0;//sframe.size.height-barHeight;
CGFloat _barHeight =
dHeightBar;
MU_tabButton* btn1 = [[MU_tabButtonalloc]
initWithFrame:CGRectMake(0, btnTop, btnWidth, _barHeight)];
[btn1 addTarget:selfaction:@selector(selectAllClicked:)forControlEvents:UIControlEventTouchUpInside];
[btn1 setBgImg:dImgTabBgandIconImg:dImgSelectAllandTitle:@"全选"withColor:dColorTabTextNormalforState:UIControlStateNormal];
MU_tabButton* btn2 = [[MU_tabButtonalloc]
initWithFrame:CGRectMake(btnWidth, btnTop, btnWidth, _barHeight)];
[btn2 setBackgroundImage:dImgTabClickedforState:UIControlStateHighlighted];
[btn2 addTarget:selfaction:@selector(deleteClicked:)forControlEvents:UIControlEventTouchUpInside];
[btn2 setBgImg:dImgTabBgandIconImg:dImgDeleteandTitle:@"删除"withColor:dColorTabTextNormalforState:UIControlStateNormal];
[bottomBaraddSubview:btn1];
[bottomBaraddSubview:btn2];
///////////其中MU_tabButton 为自定义类型
主要属性包含如下:
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
// Initialization code
CGFloat iconHeight = frame.size.height*3/5;
//文字
titleLabel = [[UILabelalloc]
initWithFrame:CGRectMake(0, iconHeight, frame.size.width, frame.size.height-
iconHeight)];
titleLabel.backgroundColor = [UIColorclearColor];
titleLabel.textColor = [UIColorwhiteColor];
titleLabel.textAlignment =NSTextAlignmentCenter;
titleLabel.font = [UIFontsystemFontOfSize:13];
[selfaddSubview:titleLabel];
//图标
CGFloat iconGap =
3;
CGFloat iconSize = iconHeight - iconGap;//图标大小
iconView = [[UIImageViewalloc]
initWithFrame:CGRectMake((frame.size.width- iconSize)/2, iconGap, iconSize, iconSize)];
[selfaddSubview:iconView];
}
return
self;
}
-(void)setBgImg:(UIImage *)bgImg andIconImg:(UIImage *)img andTitle:(NSString
*)title withColor:(UIColor*)tColor forState:(UIControlState)state
{
[selfsetBackgroundImage:bgImg
forState:UIControlStateNormal];
iconView.image = img;
titleLabel.text = title;
titleLabel.textColor = tColor;
}
-(void)setBadgeValue:(NSString*)val atTabIndex:(int)index
{
// UITabBarItem* tab = [[bottomBtnBar items] objectAtIndex:2*index];
UITabBarItem* tab = [[bottomBtnBar
items] objectAtIndex:index];
if ([val integerValue] <=
0) {
tab.badgeValue =
nil;
}
else
{
tab.badgeValue = val;
}
}
-(NSString*)getBadgeValueAtIndex:(int)index
{
UITabBarItem* tab = [[bottomBtnBar
items] objectAtIndex:index];
return tab.badgeValue;
}
相关文章推荐
- UItabBar设置的一些设置 合一些效果实现 设置badgeValue的方法封装
- Thinkphp封装P方法。实现分页效果
- iOS之旅--用CollectionView实现轮播图效果+封装方法
- Activity透明/半透明效果的设置transparent(两种实现方法)
- 封装js实现Java的trim()方法效果
- Activity透明/半透明效果的设置transparent(两种实现方法)【转】
- unity 实现物体破碎效果的一些方法 - 细雨淅淅
- Activity透明/半透明效果的设置transparent(两种实现方法)
- unity 实现物体破碎效果的一些方法
- android: 设置中文字体效果的一些方法
- jQuery封装placeholder效果实现方法,让低版本浏览器支持该效果
- Activity透明/半透明效果的设置transparent(两种实现方法)
- android: 设置中文字体效果的一些方法
- 自己重新实现QTP封装的一些常用的方法
- java中用句柄与封装基础类的方法实现C++中指针的部分效果
- Activity透明/半透明效果的设置transparent(两种实现方法)
- Activity透明/半透明效果的设置transparent(两种实现方法)
- Activity透明/半透明效果的设置transparent(两种实现方法)
- TextView设置android:ellipsize="marquee"属性,无法实现跑马灯效果的解决方法
- Activity透明/半透明效果的设置transparent(两种实现方法)