普通狀態下的文字屬性
// 普通狀態下的文字屬性
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionaryWithCapacity:0];
// 設置字體
normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
// 設置文字顏色
normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
// 設置
[vc.tabBarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
選中狀態下的文字屬性
// 選中狀態下的文字屬性
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionaryWithCapacity:0];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
// 設置
[vc.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
添加子控制器
/**
* 添加所有的子控制器
*/
- (void)setupChildVcs
{
[self setupChildVc:[[XMGEssenceViewController alloc] init] title:@"精華" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];
[self setupChildVc:[[XMGNewViewController alloc] init] title:@"新帖" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];
[self setupChildVc:[[XMGFriendTrendsViewController alloc] init] title:@"關注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];
[self setupChildVc:[[XMGMeViewController alloc] initWithStyle:UITableViewStyleGrouped] title:@"我" image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon"];
}
/**
* 添加一個子控制器
* @param title 文字
* @param image 圖片
* @param selectedImage 選中時的圖片
*/
- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
// 包裝一個導航控制器
XMGNavigationController *nav = [[XMGNavigationController alloc] initWithRootViewController:vc];
[self addChildViewController:nav];
// 設置子控制器的tabBarItem
nav.tabBarItem.title = title;
nav.tabBarItem.image = [UIImage imageNamed:image];
nav.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
}
/**
* 設置item屬性
*/
- (void)setupItem
{
// UIControlStateNormal狀態下的文字屬性
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
// 文字顏色
normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
// 文字大小
normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
// UIControlStateSelected狀態下的文字屬性
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
// 文字顏色
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
// 統一給所有的UITabBarItem設置文字屬性
// 只有后面帶有UI_APPEARANCE_SELECTOR的屬性或方法, 才可以通過appearance對象來統一設置
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
[item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}
自定義tabbaritem
#import "TTTabBar.h"
#import "TTPublishViewController.h"
@interface XMGTabBar()
/** 發布按鈕 */
@property (nonatomic, weak) UIButton *publishButton;
@end
@implementation XMGTabBar
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 設置背景圖片
self.backgroundImage = [UIImage imageNamed:@"tabbar-light"];
// 添加發布按鈕
UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
[publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
[publishButton sizeToFit];
[publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:publishButton];
self.publishButton = publishButton;
}
return self;
}
- (void)publishClick
{
TTPublishViewController *publish = [[TTPublishViewController alloc] init];
[self.window.rootViewController presentViewController:publish animated:NO completion:nil];
}
/**
* 布局子控件
*/
- (void)layoutSubviews
{
[super layoutSubviews];
// tabBar的尺寸
CGFloat width = self.width;
CGFloat height = self.height;
// 設置發布按鈕的位置
self.publishButton.center = CGPointMake(width * 0.5, height * 0.5);
// 按鈕索引
int index = 0;
// 按鈕的尺寸
CGFloat tabBarButtonW = width / 5;
CGFloat tabBarButtonH = height;
CGFloat tabBarButtonY = 0;
// 設置4個TabBarButton的frame
for (UIView *tabBarButton in self.subviews) {
if (![NSStringFromClass(tabBarButton.class) isEqualToString:@"UITabBarButton"]) continue;
// 計算按鈕的X值
CGFloat tabBarButtonX = index * tabBarButtonW;
if (index >= 2) { // 給后面2個button增加一個寬度的X值
tabBarButtonX += tabBarButtonW;
}
// 設置按鈕的frame
tabBarButton.frame = CGRectMake(tabBarButtonX, tabBarButtonY, tabBarButtonW, tabBarButtonH);
// 增加索引
index++;
}
}
@end
在自定義的TabBarController中
/**
* 處理TabBar
*/
- (void)setupTabBar
{
// 因為tabBar是只讀屬性,通過KVC進行設置
[self setValue:[[TTTabBar alloc] init] forKeyPath:@"tabBar"];
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。