自定義tabbar
#import "AppDelegate.h"
#import "ADTabBarController.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController1 * vc1 = [[ViewController1 alloc] init];
vc1.view.backgroundColor = [UIColor redColor];
ViewController2 * vc2 = [[ViewController2 alloc] init];
vc2.view.backgroundColor = [UIColor yellowColor];
ViewController3 * vc3 = [[ViewController3 alloc] init];
vc3.view.backgroundColor = [UIColor blueColor];
//分欄控制器
ADTabBarController * tab = [[ADTabBarController alloc] init];
tab.viewControllers = @[vc1, vc2, vc3];
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_window.backgroundColor = [UIColor whiteColor];
_window.rootViewController = tab;
[_window makeKeyAndVisible];
return YES;
}
#import "ADTabBarController.h"
@implementation ADTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
//隱藏系統(tǒng)tabbar
self.tabBar.hidden = YES;
//創(chuàng)建自己的tabbar
[self buildTabbar];
}
- (void)buildTabbar {
UIImageView * tabbarBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 49, [[UIScreen mainScreen] bounds].size.width, 49)];
//打開用戶交互
tabbarBottom.userInteractionEnabled = YES;
tabbarBottom.image = [UIImage imageNamed:@"header_bg"];
tabbarBottom.tag = 111;
//添加按鈕元素
NSArray * titles = @[@"消息", @"朋友", @"動態(tài)"];
NSArray * images = @[@"tab_faxian_normal", @"tab_friend_normal", @"tab_set_normal"];
NSArray * selectedImages = @[@"tab_faxian_select", @"tab_friend_select", @"tab_set_select"];
for (int i = 0; i < titles.count; i++) {
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width / titles.count * i, 0, [[UIScreen mainScreen] bounds].size.width / titles.count, 49)];
//設(shè)置標(biāo)題
[button setTitle:titles[i] forState:UIControlStateNormal];
//設(shè)置標(biāo)題顏色
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
//更改字體大小
button.titleLabel.font = [UIFont systemFontOfSize:12];
//設(shè)置圖片
[button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:selectedImages[i]] forState:UIControlStateSelected];
//綁定事件
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//容器視圖控制器和其子視圖控制器中的任意視圖tag值不能重復(fù)
button.tag = 100 + i;
//調(diào)整標(biāo)題與圖標(biāo)的位置
/*
UIKIT_STATIC_INLINE UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) {
UIEdgeInsets insets = {top, left, bottom, right};
return insets;
}
*/
//UIEdgeInsets top:向上為負(fù) 向下為正 left:向左為負(fù)灿里,向右為正 bottom:向下為負(fù),向上為正 right:向右為負(fù),向左為正 總結(jié): 上左下右: 是哪個方向就向哪個方向為負(fù)控淡,即:top:向上為負(fù)唱遭,向下為正
button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.bounds.size.width, -20, 0);
button.imageEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, -button.titleLabel.bounds.size.width);
[tabbarBottom addSubview:button];
//設(shè)置默認(rèn)選中項罗心,為0
if (i == 0) {
[self buttonClicked:button];
}
}
[self.view addSubview:tabbarBottom];
}
- (void)buttonClicked:(UIButton *)button {
NSInteger index = button.tag - 100;
self.selectedIndex = index;
//讓選中的按鈕處于選中狀態(tài)
UIImageView * imageView = (id)[self.view viewWithTag:111];
NSArray * subViews = imageView.subviews;
for (UIButton * button in subViews) {//這里循環(huán)的目的就是讓之前按鈕狀態(tài)變?yōu)榉沁x中狀態(tài)电抚,然后只選中一個之后纯衍,就是選中狀態(tài)了任岸。
[button setSelected:NO];
}
//被點擊的按鈕設(shè)置為選中狀態(tài)
[button setSelected:YES];
}
@end