#import <UIKit/UIKit.h>
@interface MainTabbarController : UITabBarController
@end
#import "MainTabbarController.h"
#import "SquareViewController.h"
#import "MallViewController.h"
#import "CommunityViewController.h"
#import "PeripheryViewController.h"
#import "MyViewController.h"
#import "XWButton.h"
@interface MainTabbarController ()
@property (nonatomic , strong ) NSArray *navigateArr;
@property (nonatomic , strong ) NSMutableArray *titleArr;
@property (nonatomic , strong ) NSMutableArray *imageArr;
@property (nonatomic , strong ) NSMutableArray *CtrlArr;
@end
@implementation MainTabbarController {
UIImageView *selectImage;
NSInteger selectTag;
}
- (void )dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"tabbarImageArr" object:nil ];
}
- (void )noti:(NSNotification *)not{
NSLog (@"%@" ,not.object);
if (not.object) {
_navigateArr = not.object;
[self _loadSubViewCtrl];
[self _removeSystemBarbtn];
[self _creatTabbarBtn];
}
}
- (void )viewDidLoad {
[super viewDidLoad];
_titleArr = [[NSMutableArray alloc]init];
_imageArr = [[NSMutableArray alloc]init];
_CtrlArr = [[NSMutableArray alloc]init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (noti:) name:@"tabbarImageArr" object:nil ];
}
- (void )viewDidAppear:(BOOL )animated{
NSLog (@"%@" ,self .tabBar.subviews);
[super viewDidAppear:animated];
[self _removeSystemBarbtn];
}
- (void )_removeSystemBarbtn{
Class class = NSClassFromString (@"UITabBarButton" );
for (UIView *view in self .tabBar.subviews) {
if ([view isKindOfClass:class ]) {
[view removeFromSuperview];
}
}
}
- (void )_creatTabbarBtn{
[_titleArr removeAllObjects];
[_imageArr removeAllObjects];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake (0 , 0 , screenWidth, 49 )];
[self .tabBar addSubview:imageView];
NSArray *titles = @[@"广场" ,@"商城" ,@"小区" ,@"周边" ,@"我" ];
for (int i=0 ; i<_navigateArr.count; i++) {
if (![[_navigateArr[i] objectForKey:@"commentCode" ] isKindOfClass:[NSNull class ]]) {
[_titleArr addObject:[titles objectAtIndex:[[_navigateArr[i] objectForKey:@"commentCode" ] integerValue]]];
}
if (![[_navigateArr[i] objectForKey:@"kindIcon" ] isKindOfClass:[NSNull class ]]) {
[_imageArr addObject:[_navigateArr[i] objectForKey:@"kindIcon" ]];
}else {
[_imageArr addObject:@"" ];
}
}
for (NSInteger i = 0 ; i < _titleArr.count; i++) {
NSString *title = _titleArr[i];
NSString *imgName = _imageArr[i];
XWButton *btn = [XWButton buttonWithType:UIButtonTypeCustom ];
btn.frame = CGRectMake (screenWidth/_titleArr.count * i, 0 , screenWidth / _titleArr.count, 49 );
[btn setTitle:title forState:UIControlStateNormal ];
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal ];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected ];
[btn sd_setImageWithURL:[NSURL URLWithString:imgName] forState:UIControlStateNormal completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
UIImage *cImageV = [self imageCompressForSize:image targetSize:CGSizeMake (25 , 25 )];
[btn setImage:cImageV forState:UIControlStateNormal ];
}];
btn.titleLabel.font = [UIFont systemFontOfSize:14 ];
[btn addTarget:self action:@selector (btnAction:) forControlEvents:UIControlEventTouchUpInside ];
btn.tag = i + 100 ;
[self .tabBar addSubview:btn];
if (i == 0 ) {
btn.selected = YES ;
selectTag = 100 ;
}
}
}
- (void )btnAction:(UIButton *)btn{
self .selectedIndex = btn.tag - 100 ;
if (btn.tag!=selectTag) {
btn.selected = YES ;
UIButton *selectBtn = [self .tabBar viewWithTag:selectTag];
selectBtn.selected = NO ;
}
if (btn.tag==selectTag) {
btn.selected = YES ;
}
selectTag = btn.tag;
}
#pragma -mark 加载子控制器
- (void )_loadSubViewCtrl{
[_CtrlArr removeAllObjects];
SquareViewController *SquareVC = [[SquareViewController alloc] init];
MallViewController *MallVC = [[MallViewController alloc] init];
CommunityViewController *CommunityVC = [[CommunityViewController alloc] init];
PeripheryViewController *PeripheryVC = [[PeripheryViewController alloc] init];
MyViewController *MyVC=[[MyViewController alloc]init];
NSArray *viewCtrls = @[SquareVC,MallVC,CommunityVC,PeripheryVC,MyVC];
NSMutableArray *navs = [NSMutableArray array];
for (int i=0 ; i<_navigateArr.count; i++) {
if (![[_navigateArr[i] objectForKey:@"commentCode" ] isKindOfClass:[NSNull class ]]) {
[_CtrlArr addObject:[viewCtrls objectAtIndex:[[_navigateArr[i] objectForKey:@"commentCode" ] integerValue]]];
}
}
for (NSInteger i = 0 ; i < _CtrlArr.count; i++) {
UIViewController *viewCtrl = [_CtrlArr objectAtIndex:i];
UINavigationController *childVcNav = [[UINavigationController alloc]initWithRootViewController:viewCtrl];
childVcNav.navigationBar.translucent = YES ;
[navs addObject:childVcNav];
}
self .viewControllers = navs;
}
- (UIImage *)imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize )size{
UIImage *newImage = nil ;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = size.width;
CGFloat targetHeight = size.height;
CGFloat scaleFactor = 0.0 ;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake (0.0 , 0.0 );
if (CGSizeEqualToSize (imageSize, size) == NO ){
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor){
scaleFactor = widthFactor;
}
else {
scaleFactor = heightFactor;
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
if (widthFactor > heightFactor){
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5 ;
}else if (widthFactor < heightFactor){
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5 ;
}
}
UIGraphicsBeginImageContextWithOptions (size, NO , 3.0 );
CGRect thumbnailRect = CGRectZero ;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext ();
if (newImage == nil ){
NSLog (@"scale image fail" );
}
UIGraphicsEndImageContext ();
return newImage;
}
@end