微博的TabBar

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];

[self loadViewController];

return YES;

}

//加载视图控制器

- (void)loadViewController {

MyTabBarController *tabVC = [[MyTabBarController alloc] init];

//设置选中时显示的文字的颜色(前景色)

tabVC.tabBar.tintColor= [UIColor orangeColor];

//设置代理可以调用返回值为BOOL的方法

tabVC.delegate = self;

//首页

//imageWithRenderingMode图片渲染模式

//UIImageRenderingModeAlwaysOriginal不使用tintColor进行渲染

ViewController *hvc = [[ViewController alloc] init];

UIImage *hImg1 = [[UIImage imageNamed:@"tabbar_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UIImage *hImg2 = [[UIImage imageNamed:@"tabbar_home_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

hvc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:hImg1 selectedImage:hImg2];

//发现

FindViewController *fvc = [[FindViewController alloc]init];

UIImage *fImg1 = [[UIImage imageNamed:@"tabbar_discover"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UIImage *fImg2 = [[UIImage imageNamed:@"tabbar_discover_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

fvc.tabBarItem= [[UITabBarItem alloc] initWithTitle:@"发现" image:fImg1 selectedImage:fImg2];

//发布(占位VC)

UIViewController *vc = [[UIViewController alloc] init];

vc.view.backgroundColor= [UIColor purpleColor];

//数组中元素按照显示的顺序进行放置

tabVC.viewControllers= @[hvc,vc,fvc];

//设置根视图控制器否则运行时系统会崩溃

self.window.rootViewController = tabVC;

}

#pragma mark --- UITabBarControllerDelegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

//对于自定义的tabBar,要对这个方法进行调用时,不仅需要导入代理,设置,还需要对触发这个方法的按钮进行注册,不然不会触发该方法

//接收按钮视图

//UIControl *control = obj;

//注册绑定事件

//[control sendActionsForControlEvents:UIControlEventTouchUpInside];

//设置占位VC的视图控制器 012

if([tabBarController.viewControllers indexOfObject: viewController] == 1) {

AnimationVC *avc = [[AnimationVC alloc]init];

//模态弹出样式

avc.modalPresentationStyle = UIModalPresentationCurrentContext;

//视图模态出现

[tabBarController presentViewController:avcanimated:YES completion:nil];

//使占位页面不出现

return NO;

}

//使点击事件可以触发

return YES;

}


@implementation MyTabBarController

- (void)viewDidLoad {

[super viewDidLoad];

//使用自定义tabBar

//self.tabBar

//KVC键值编码这里的key不能出错key valve coding

[self setValue:[CustomTabBarnew] forKey:@"tabBar"];

}


@implementation CustomTabBar

- (id)initWithFrame:(CGRect)frame {

self= [super initWithFrame:frame];

if(self) {

//添加到tabBar使用get方法创建,所以这里要set打点调用

[self addSubview:self.btn];

}

return self;

}

//重新布局子视图

- (void)layoutSubviews {

[supe rlayoutSubviews];

//让btn显示到最上面

[self bringSubviewToFront:_btn];

}

- (UIButton *)btn {

if(!_btn) {

_btn = [UIButton buttonWithType:UIButtonTypeCustom];

_btn.frame= CGRectMake((WIDTH- 50) / 2.0, 2, 50, 40);

//背景图片

[_btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];

[_btns etBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];

//加号图片

[_btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];

[_btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];

[_btn addTarget:selfaction:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];

}

return _btn;

}

- (void)Click:(UIButton *)sender {

//在本视图共有6层视图(012345)遍历所有子视图找到按钮视图

[self.subviews enumerateObjectsUsingBlock:^(__kindofUIView *_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {

//找到加号按钮

if(idx == 2) {

//接收按钮视图

UIControl *control = obj;

//注册绑定事件

[control sendActionsForControlEvents:UIControlEventTouchUpInside];

}

}];

}

你可能感兴趣的:(微博的TabBar)