先上效果图我的项目在iOS6中的效果
/*这里设置颜色是为了Navigtaion上面的颜色ButtonItem的颜色可以和Navigtaion的主色调更接近*/
[[UINavigationBar appearance] setTintColor:color];
/*这里我有个自己的方法,我讲blankImage 着色成我要的颜色,blankImage其实就是一个白色的小方块,我把它改编成我要的颜色,然后作为Navigtaion的背景图*/
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"blankImage"] imageWithTintColor:color ] forBarMetrics:UIBarMetricsDefault];
/*这个比较特殊,这里我加入了几个属性应该一下就可以看懂 一个是修改标题的颜色 ,一个是字体大小。当然还有其他属性可以自行翻阅API*/
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor blackColor], UITextAttributeTextColor,[UIFont systemFontOfSize:17.0f],UITextAttributeFont, nil]];
到这里NavigtaionBar修改的有点像iOS7的了
下面看代码
/*这里我找到当前的tabBar 然后设置他的选中的时候图片为透明,酱紫选中也不会有白色的框了*/
[mainTabs.tabBar setSelectionIndicatorImage:[[UIImage imageNamed:@"blankImage"] imageWithTintColor:[UIColor clearColor]]];
/*然后设置背景图,颜色就是iOS7tabbar默认的那种白色 #21262d*/
[mainTabs.tabBar setBackgroundImage:[[[UIImage imageNamed:@"blankImage"] imageWithTintColor:[Util GetColorWithHexString:@"#21262d"]] stretchableImageWithLeftCapWidth:10 topCapHeight:10]];
[items.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [Util GetColorWithHexString:@"#929292"], UITextAttributeTextColor,[UIFont boldSystemFontOfSize:11.f],UITextAttributeFont, nil] forState:UIControlStateNormal];
[items.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Global GetDarkMainColor], UITextAttributeTextColor,[UIFont boldSystemFontOfSize:11.f],UITextAttributeFont, nil] forState:UIControlStateSelected];
UIImage *selectedImage = [[UIImage imageNamed:tabImgArr[i]] imageWithTintColor:[Global GetDarkMainColor]];
UIImage *unselectedImage = [[UIImage imageNamed:tabImgArr[i]] imageWithTintColor:[Util GetColorWithHexString:@"#929292"]];
[items.tabBarItem setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
[[UIToolbar appearance] setBackgroundImage:[[UIImage imageNamed:@"blankImage"] imageWithTintColor:color ] forToolbarPosition:0 barMetrics:0];
我刚开始对于这个也纠结了很久,最后我还是想了个这种的方案,不使用原来的按钮了,我自定义一下。
可以参照我的代码:
/**
* 利用个一个UIButton对象生成一个UIBarButtonItem
* @param rect 这个按钮的大小
* @param btn 要放到这个UIBarButtonItem里面的UIButton
* @return 一个UIBarButtonItem对象
*/
+(UIBarButtonItem*)getNavBarButtonItem:(CGRect)rect Btn:(UIButton*)btn;
/**
* 通过 title、执行的SEL、选中颜色、未选中颜色生成一个UIBarButtonItem
* @param title UIBarButtonItem显示的ttitle文字
* @param target 调用的目标 可以为nil
* @param sel 执行事件
* @param selectColor 选中颜色
* @param normalColor 未选中颜色
*
* @return 一个UIBarButtonItem对象
*/
+(UIBarButtonItem*)getNavBarButtonItemWithTitle:(NSString *)title Target:(id)target Selector:(SEL)sel SelectColor:(UIColor *)selectColor NormalColor:(UIColor*)normalColor;
/**
* 通过 img、执行的SEL、选中颜色、未选中颜色生成一个UIBarButtonItem
* @param img 图片
* @param target 执行sel的目标类
* @param sel 需要执行的方法
* @param selectColor 选中颜色,默认是IOS7的蓝色
* @param normalColor 普通状态下颜色 默认是白色
* @param leftOrRight 该参数只有在IOS7中起效 @"left" @"right"
*
* @return 返回一个UIBarButtonItem按钮
*/
+(UIBarButtonItem*)getNavBarButtonItemWithImage:(UIImage *)img Target:(id)target Selector:(SEL)sel SelectColor:(UIColor *)selectColor NormalColor:(UIColor*)normalColor;
+(UIBarButtonItem*)getNavBarButtonItem:(CGRect)rect Btn:(UIButton*)btn
{
UIView * v= [[UIView alloc]initWithFrame:rect];
[v addSubview:btn];
UIBarButtonItem *btnItem=[[UIBarButtonItem alloc]initWithCustomView:btn];
return btnItem;
}
+(UIBarButtonItem*)getNavBarButtonItemWithImage:(UIImage *)img Target:(id)target Selector:(SEL)sel SelectColor:(UIColor *)selectColor NormalColor:(UIColor*)normalColor
{
UIButton * btn ;
if(selectColor==nil)
selectColor=[Global getIOS7TextColor];
if(normalColor==nil)
normalColor=[UIColor whiteColor];
btn= [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[btn setBackgroundImage:[img imageWithTintColor:normalColor] forState:UIControlStateNormal];
[btn setBackgroundImage:[img imageWithTintColor:selectColor] forState:UIControlStateHighlighted];
[btn addTarget:target action:sel forControlEvents:UIControlEventTouchDown];
return [self getNavBarButtonItem:CGRectMake(0, 0, 45, 45) Btn:btn];
}
+(UIBarButtonItem*)getNavBarButtonItemWithTitle:(NSString *)title Target:(id)target Selector:(SEL)sel SelectColor:(UIColor *)selectColor NormalColor:(UIColor*)normalColor
{
if(selectColor==nil)
selectColor=[Global getIOS7TextColor];
if(normalColor==nil)
normalColor=[UIColor whiteColor];
if(ISIOS7)
{
UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 5, 50, 35)];
[btn setTitle:title forState:UIControlStateNormal];
[btn setTitle:title forState:UIControlStateHighlighted];
[btn setTitleColor:normalColor forState:UIControlStateNormal];
[btn setTitleColor:selectColor forState:UIControlStateHighlighted];
[btn addTarget:target action:sel forControlEvents:UIControlEventTouchDown];
return [self getNavBarButtonItem:CGRectMake(0, 0, 50, 45) Btn:btn];
}
else
{
return [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:target action:sel];
}
}