$(SRCROOT)/BiShe/PrefixHeader.pch
- 自定义分栏控制器 UITabBarController
@implementation BSTabBarController
- (instancetype)init
{
self = [super init];
if (self) {
[self setUpItemTextAttrs];
[self setUpAllChildViewControllers];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
/**
* 统一设置Item文字的属性
*/
- (void)setUpItemTextAttrs{
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
normalAttrs[NSFontAttributeName] = Font_20;
NSMutableDictionary *selectAttrs = [NSMutableDictionary dictionary];
selectAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
[item setTitleTextAttributes:selectAttrs forState:UIControlStateSelected];
}
/**
* 添加所有子控制器
*/
- (void)setUpAllChildViewControllers{
[self setUpOneViewController:[[BSHomeViewController alloc]init] title:@"首页" image:@"tabbar_home_nor" selectImage:@"tabbar_home_pre"];
[self setUpOneViewController:[[BSCategoryViewController alloc]init] title:@"分类" image:@"tabbar_category_nor" selectImage:@"tabbar_category_pre"];
[self setUpOneViewController:[[BSShopCartViewController alloc]init] title:@"购物车" image:@"tabbar_shoppingcart_nor" selectImage:@"tabbar_shoppingcart_pre"];
[self setUpOneViewController:[[BSAccountViewController alloc]init] title:@"个人中心" image:@"tabbar_mine_nor" selectImage:@"tabbar_mine_pre"];
}
/**
* 添加一个子控制器
*/
- (void)setUpOneViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage
{
if ([title isEqualToString:@"首页"]) {
UIImageView * imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(0, 0, 40, 40);
[imageView setImage:[UIImage imageNamed:@"home_logo"]];
vc.navigationItem.titleView = imageView;
}
vc.title = title;
vc.tabBarItem.image = [UIImage imageNamed:image];
vc.tabBarItem.selectedImage = [UIImage imageNamed:selectImage];
BSNavigationController *nav = [[BSNavigationController alloc]initWithRootViewController:vc];
[self addChildViewController:nav];
}
- 继承UINavigationController的BSNavigationController,自定义导航栏,包括返回按钮,右滑返回手势
@interface BSNavigationController ()<UIGestureRecognizerDelegate>
@end
@implementation BSNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
self.interactivePopGestureRecognizer.delegate = self;
}
/**
* 统一设置左上角返回图片
*/
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.childViewControllers.count > 0) {
viewController.hidesBottomBarWhenPushed = YES;
viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(blackBtnClick) image:@"commom_back" highImage:@"commom_back"];
}
[super pushViewController:viewController animated:animated];
}
- (void)blackBtnClick{
[self popViewControllerAnimated:YES];
}
#pragma mark -- 点击空白处收起键盘
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self.view endEditing:YES];
}
#pragma mark - <UIGestureRecognizerDelegate>
/**
* push进来的控制器大于1个,手势有效
*/
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return self.viewControllers.count > 1;
}
@end
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:Font_30,NSForegroundColorAttributeName:Color_2F2F2F}];
#if DEBUG
#define DR_NSLog(fmt,...) NSLog((@"%s [Line %d] " fmt),__PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); /**<输出语句*/
#else
#define DR_NSLog(fmt, ...)
#endif
#define kiPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define kiPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define kiPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define kiPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
#define BSColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define BSRandomColor BSColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))
- 重写
UIBarButtonItem
分栏标题与图片
/**
* 创建一个item
*
* @param target 点击item后调用哪个对象的方法
* @param action 点击item后调用target的哪个方法
* @param image 图片
* @param highImage 高亮的图片
*
* @return 创建完的item
*/
+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image highImage:(NSString *)highImage
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 30)];
btn.size = CGSizeMake(60, 44);
return [[UIBarButtonItem alloc] initWithCustomView:btn];
}