自定义TabBar步骤

来源:互联网 发布:爱剪辑mac版中文版 编辑:程序博客网 时间:2024/06/05 03:32

现在大部分的应用都会用到底部导航,那么如何自定义底部导航呢?废话少说,开始!

1、新建TabBarController累

2、移除自身的tabar        [self.tabar removeFromSuperview]

3、自定义tabarView,用来充当他自己的底部view,    自定义底部的四个按钮,充当他自己的底部按钮,结合view和button,自定义的tabar就出来了,然后将tabarview加在TabBarController的底部即可


上代码:

//自定义View

#import <UIKit/UIKit.h>


@interface TabBarView : UIView


@property(nonatomic,strong)NSString *barvalue;


-(void)initTabBarViewWithNumOfButton:(int)num andBackImage:(NSArray *)backImageArray andseledtedImage:(NSArray*)backSelectedImages andImageFrame:(CGRect)imageFrame andTitle:(NSArray*)title andLableFrame:(CGRect)lableFrame andSelectedIndex:(int)index;


-(void)initTabBarViewWithNumOfButton:(int)num andBackImage:(NSArray *)backImageArray andseledtedImage:(NSArray*)backSelectedImages andSelectedIndex:(int)index;


@property(nonatomic,strong)NSMutableArray *buttons;

@end





#import "TabBarView.h"

#import "TabBarButton.h"

#import "BBBadgeBarButtonItem.h"

#import "Colours.h"

#import "umengStatistic.h"

@interface TabBarView()



@property(nonatomic,strong)TabBarButton *selectedButton;

@property(nonatomic,strong)NSArray *backImages;

@property(nonatomic,strong)NSArray *selectedImages;


@property(strong,nonatomic)BBBadgeBarButtonItem *barButton;



@end



@implementation TabBarView


-(void)initTabBarViewWithNumOfButton:(int)num andBackImage:(NSArray *)backImageArray andseledtedImage:(NSArray*)backSelectedImages andImageFrame:(CGRect)imageFrame andTitle:(NSArray*)title andLableFrame:(CGRect)lableFrame andSelectedIndex:(int)index{

    

    self.buttons = [NSMutableArrayarray];

    self.backImages = backImageArray;

    self.selectedImages = backSelectedImages;

    

    if (num>0) {

        for (int i = 0; i<num; i++) {

            TabBarButton *tabBarButton = [[TabBarButtonalloc]initWithFrame:CGRectMake(i*VIEW_W/(num) , 20, VIEW_W/num, self.frame.size.height - 20)];

            [tabBarButton initTabBarButton:nilwithBackImage:backImageArray[i]andImageFrame:imageFrameandTitle:title[i] andTitleFrame:lableFrame];

            if (i == 1) {

                self.barButton = [[BBBadgeBarButtonItemalloc] initWithCustomUIButton:tabBarButton];

                self.barButton.badgeOriginX = 46;

                self.barButton.badgeOriginY = 18;


            }

            if (i == 2) {

                tabBarButton.frame =CGRectMake(i* VIEW_W/(num) , 10,VIEW_W/num, self.frame.size.height - 10);

                tabBarButton.TitleLabel.frame =CGRectMake(lableFrame.origin.x, lableFrame.origin.y - 10, lableFrame.size.width, lableFrame.size.height);

            }

            tabBarButton.tag = i;

            [tabBarButton addTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];

            [selfaddSubview:tabBarButton];

            [self.buttonsaddObject:tabBarButton];

            

            tabBarButton.TitleLabel.textColor = [UIColorcolorWithRed:121.0/255 green:126.0/255 blue:135.0/255alpha:1];

            

            if (i == index) {

                tabBarButton.backImageView.image = backSelectedImages[index];

                self.selectedButton = tabBarButton;

                tabBarButton.TitleLabel.textColor = [UIColorcolorFromHexString:@"447dc0"];

                [[NSNotificationCenterdefaultCenter]postNotificationName:@"TabBar"object:[NSStringstringWithFormat:@"%d",i]userInfo:nil];

            }

        }

    }

}



-(void)initTabBarViewWithNumOfButton:(int)num andBackImage:(NSArray *)backImageArray andseledtedImage:(NSArray*)backSelectedImages andSelectedIndex:(int)index{

    

    self.buttons = [NSMutableArrayarray];

    self.backImages = backImageArray;

    self.selectedImages = backSelectedImages;

    

    if (num>0) {

        for (int i = 0; i<num; i++) {

            TabBarButton *tabBarButton = [[TabBarButtonalloc]initWithFrame:CGRectMake(i*VIEW_W/(num) , 0,VIEW_W/num,self.frame.size.height)];

            tabBarButton.tag = i;

            [tabBarButton addTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];

            [tabBarButton initTabBarButton:nilandBackImage:backImageArray[i]];

            [selfaddSubview:tabBarButton];

            [self.buttonsaddObject:tabBarButton];

            

            if (i == index) {

                

                tabBarButton.backImageView.image = backSelectedImages[index];

                self.selectedButton = tabBarButton;

                [[NSNotificationCenterdefaultCenter]postNotificationName:@"TabBar"object:[NSStringstringWithFormat:@"%d",i]userInfo:nil];

            }

        }

    }

}



-(void)click:(TabBarButton*)sender{

    

    if ([senderisEqual:self.selectedButton]) {

    }else{

        //统计点击次数

        if(sender.tag == 0)

        {

            [umengStatisticumengEvent:@"event_home"attributes:@{@"name":@"iPhone",@"number":@"1"}number:@(1)];

        }

        elseif (sender.tag == 1)

        {

            [umengStatisticumengEvent:@"event_communication"attributes:@{@"name":@"iPhone",@"number":@"2"}number:@(2)];

        }

        elseif (sender.tag == 2)

        {

            [umengStatisticumengEvent:@"event_today"attributes:@{@"name":@"iPhone",@"number":@"3"}number:@(3)];

        }

        elseif (sender.tag == 3)

        {

            [umengStatisticumengEvent:@"event_myFavorite"attributes:@{@"name":@"iPhone",@"number":@"4"}number:@(4)];

        }

        else

        {

            [umengStatisticumengEvent:@"event_preference"attributes:@{@"name":@"iPhone",@"number":@"5"}number:@(5)];

        }

        sender.backImageView.image =self.selectedImages[sender.tag];

        self.selectedButton.backImageView.image=self.backImages[self.selectedButton.tag];

        

//        sender.TitleLabel.textColor = [UIColor colorFromHexString:@"447dc0"];

        self.selectedButton.TitleLabel.textColor = [UIColor colorWithRed:121.0/255green:126.0/255 blue:135.0/255alpha:1];

    }

    

    [[NSNotificationCenterdefaultCenter]postNotificationName:@"TabBar"object:[NSStringstringWithFormat:@"%ld",(long)sender.tag]userInfo:nil];

    self.selectedButton = sender;

}



//自定义Button

#import <UIKit/UIKit.h>


@interface TabBarButton : UIButton


@property (nonatomic,strong)UIImage *backImage;

@property (nonatomic,strong)NSString  *Title;

@property (nonatomic,strong)UIImageView *backImageView;

@property (nonatomic,strong)UILabel  *TitleLabel;



-(void)initTabBarButton:(TabBarButton *)tabBarButton withBackImage:(UIImage *)backImage andImageFrame:(CGRect)frame andTitle:(NSString *)title andTitleFrame:(CGRect)frame1;


-(void)initTabBarButton:(TabBarButton *)tabBarButton andBackImage:(UIImage *)backImage;


@end



#import "TabBarButton.h"


@implementation TabBarButton


-(void)initTabBarButton:(TabBarButton *)tabBarButton withBackImage:(UIImage *)backImage andImageFrame:(CGRect)frame andTitle:(NSString *)title andTitleFrame:(CGRect)frame1;{

    

    self.backImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(frame.origin.x, frame.origin.y - 20, frame.size.width, frame.size.height)];

    self.backImageView.image = backImage;

    self.TitleLabel = [[UILabelalloc]initWithFrame:CGRectMake(frame1.origin.x, frame1.origin.y - 20, frame1.size.width, frame1.size.height)];

    self.TitleLabel.textAlignment =NSTextAlignmentCenter;

    self.TitleLabel.font = [UIFontboldSystemFontOfSize:12];

    self.TitleLabel.text = title;

    [selfaddSubview:self.backImageView];

    [selfaddSubview:self.TitleLabel];

    

}



-(void)initTabBarButton:(TabBarButton *)tabBarButton andBackImage:(UIImage *)backImage {

    

    self.backImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0, 0,self.frame.size.width,self.frame.size.height)];

    self.backImageView.image = backImage;

    [selfaddSubview:self.backImageView];

}

@end



//tabarController

-(void)initTabBarView{

    

    NSArray *imageArr = [[NSArrayalloc]initWithObjects:[UIImageimageNamed:@"app_center_normal"], [UIImageimageNamed:@"communication_normal"], [UIImageimageNamed:@"today_normal"], [UIImageimageNamed:@"community_normal"], [UIImageimageNamed:@"my_normal"],nil];

    NSArray *selectedImage = [[NSArrayalloc]initWithObjects:[UIImageimageNamed:@"app_center_seleted"], [UIImageimageNamed:@"communication_seleted"], [UIImageimageNamed:@"today_seleted"], [UIImageimageNamed:@"community_seleted"], [UIImageimageNamed:@"my_seleted"],nil];

    

    

    TabBarView *tabBarView = [[TabBarViewalloc]initWithFrame:CGRectMake(0,self.view.frame.size.height - 50 - 20, self.view.frame.size.width, 50 + 20)];

    tabBarView.barvalue = [NSStringstringWithFormat:@"%d", [self.barvalueintValue]];

    

    UIImageView *imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 70)];

    imageView.image = [UIImageimageNamed:@"底栏背景.png"];

    [tabBarView addSubview:imageView];

    [self.viewaddSubview:tabBarView];

    

    HomePageViewController *homeVC = [[HomePageViewControlleralloc]init];

    communicationViewController *instantVC = [[communicationViewControlleralloc]init];

    TodayController *todayVC = [[TodayControlleralloc]init];

    MyFavoriteViewController *favoriteVC = [[MyFavoriteViewControlleralloc]init];                                    

     PreferencesViewController *preferenceVC = [[PreferencesViewControlleralloc]init];

    self.viewControllers = [[NSArrayalloc]initWithObjects:homeVC,instantVC, todayVC, favoriteVC,preferenceVC,nil];

    

    //设置文字

    NSArray* titlearr = [NSArrayarrayWithObjects:@"应用中心",@"沟通",@"今日事",@"社区",@"我的",nil];

    [tabBarView initTabBarViewWithNumOfButton:5andBackImage:imageArr andseledtedImage:selectedImage andImageFrame:CGRectMake((VIEW_W/5 - 25)/2 - 2.5, 20, 30, 30)andTitle:titlearr andLableFrame:CGRectMake(0, 31 + 20,VIEW_W/5, 15)andSelectedIndex:2];

    

    UIButton* goutongBtn = tabBarView.buttons[1];

    self.barButton = [[BBBadgeBarButtonItemalloc] initWithCustomUIButton:goutongBtn];

    self.barButton.badgeOriginX = goutongBtn.frame.size.width-30;

    self.barButton.badgeOriginY = 0;

    

    

    self.comBarButton = tabBarView.buttons[3];

    self.barView = [[UIViewalloc]initWithFrame:CGRectMake(self.comBarButton.frame.size.width-27, 2, 10, 10)];

    self.barView.backgroundColor = [UIColorredColor];

    self.barView.layer.cornerRadius = 5;

    self.barView.clipsToBounds =YES;

}



因为是给自己公司写的项目  所以把代码直接粘过来了,有些乱


0 0
原创粉丝点击