因为最近项目高仿新浪微博,所以我决定写一个新浪的UI,因为之前都是纯使用微博,从来没有细想过微博的实现过程,一开始凭感觉做,做出来发现里面还是一些比较恶心的地方。从头开讲。
一开始还是写tabBarController,因为微博的发布按钮与其他按钮显然的区别,所以觉得要定制tabBarController,但是后来一看,尼玛。。。。新浪用的原生tabBarController,好吧!虽然顶自己定制也能做的一波一样,但是太麻烦!于是,这里就出现了两种方式,韩老板的方法是重写tabBar,用tabBar的layoutSubviews重绘按钮位置。我用的方式则是创建一个假的controller占位,然后创建一个Button加在tabBar上,去覆盖controller按钮的位置。这两种方式都能实现功能!下面就用我的方法来讲如何实现微博的TabBarController。
首先我们self.window的根视图是一个NavigationController,而不是tabBarController。
UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:[YHRootViewController new]];
self.window.rootViewController = nav;
接下来是在tabBarController里做事情了。直接上代码,备注写好了的。
#import "YHRootViewController.h"
#import "YHPublishButton.h"
#import "YHHomeViewController.h"
#import "YHMessageViewController.h"
#import "YHPublishViewController.h"
#import "YHDiscoverViewController.h"
#import "YHMeViewController.h"
#import "YHChoosePublishWayView.h"
#import "YHUIFactory.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface YHRootViewController ()<UITabBarControllerDelegate>
{
YHPublishButton * _publishButton;
}
@end
@implementation YHRootViewController
- (void)viewDidLoad {
[super viewDidLoad];
/**
* 将item上选中是字体设置为——橙色
*/
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor], NSForegroundColorAttributeName,nil] forState:UIControlStateSelected];
[self loadControllers];
[self addPublishButton];
self.delegate = self;
}
- (void)loadControllers
{
NSArray * viewControllerNameArray = @[@"YHHomeViewController",@"YHMessageViewController",@"YHPublishViewController",@"YHDiscoverViewController",@"YHMeViewController"];
NSArray * viewControllerTitleArray = @[@"主页",@"消息",@"",@"发现",@"我的"];
NSArray * viewControllerBgArray = @[@"tabbar_home",@"tabbar_message_center",@"不存在的图片",@"tabbar_discover",@"tabbar_profile"];
NSArray * viewControllerSelectBgArray = @[@"tabbar_home_selected",@"tabbar_message_center_selected",@"不存在的图片",@"tabbar_discover_selected",@"tabbar_profile_selected"];
NSMutableArray * viewControllers = [[NSMutableArray alloc] init];
/**
* 工厂模式创建viewController
*/
for (int i = 0; i < viewControllerNameArray.count; i++) {
UIViewController * tempViewController = [YHUIFactory createViewController:viewControllerNameArray[i]];
tempViewController.tabBarItem.title = viewControllerTitleArray[i];
tempViewController.tabBarItem.image = [[UIImage imageNamed:viewControllerBgArray[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tempViewController.tabBarItem.selectedImage = [[UIImage imageNamed:viewControllerSelectBgArray[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[viewControllers addObject:tempViewController];
};
/**
* 加入viewControllers
*/
self.viewControllers = [viewControllers copy];
}
- (void)addPublishButton
{
float w = 60;
float h = 45;
/**
发布按钮
*/
_publishButton = [[YHPublishButton alloc] initWithFrame:(CGRectMake(( SCREEN_WIDTH- w) / 2, 2 , w, h))];
[_publishButton setTitle:@"+" forState:(UIControlStateNormal)];
_publishButton.titleLabel.font = [UIFont systemFontOfSize:50.0];
[_publishButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:(UIControlStateNormal)];
[_publishButton addTarget:self action:@selector(publishGo:) forControlEvents:(UIControlEventTouchUpInside)];
[self.tabBar addSubview:_publishButton];
}
- (void)publishGo:(UIButton *)sender
{
YHChoosePublishWayView * cp = [[YHChoosePublishWayView alloc] initWithFrame:(CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))];
[self.view addSubview:cp];
/**
* 选择发布内容的页面Block
*
* @param num 判断是那种方式发布
*/
cp.publishBlock = ^(int num)
{
/**
* 以模态方式推出
*/
[self presentViewController:[YHPublishViewController new] animated:YES completion:nil];
};
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
/**
* 清空消息提示
*/
viewController.tabBarItem.badgeValue = nil;
}
先写这么多,开始写公司项目了。。。。烦。。。。