自定义Tabbar方法—

来源:互联网 发布:最优化导论中文版 编辑:程序博客网 时间:2024/04/26 04:07

方法一    覆盖式

原理:在Tabbar上放button,让button覆盖掉tabbaritem,再给button添加事件,需要注意的地方是button的坐标

代码如下:

头文件

#import <UIKit/UIKit.h>


@interface BaseTabBarController :UITabBarController


-(void) addCenterButtonWithImage:(UIImage*)buttonImage

                  highlightImage:(UIImage*)highlightImage;


- (void)addLeftButtonWithImage:(UIImage *)buttonImage

                highlightImage:(UIImage *)highlightImage;


- (void)addRightButtonWithImage:(UIImage *)buttonImage

                 highlightImage:(UIImage *)hightlightImage;


@end


.m文件


#import "BaseTabBarController.h"


#define kBtnLeftTag     0

#define kBtnRightTag    2

#define kBtnCenterTag   1


constint width =100.0f;

constfloat centerBtnWidth =67.0f;

constfloat centerBtnHeight =66.0f;

constfloat btnWidth =127.0f;

constfloat btnHeight =49.0f;


@interface BaseTabBarController ()


@end


@implementation BaseTabBarController


- (void)viewDidLoad

{

    [superviewDidLoad];

    self.tabBar.backgroundColor = [UIColorbrownColor];

    [self.tabBarsetBackgroundImage:[UIImageimageNamed:@"tabbarBackground.png"]];

}


-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage

{

    UIButton* button = [UIButtonbuttonWithType:UIButtonTypeCustom];

    button.tag =kBtnCenterTag;

    button.autoresizingMask =UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin |UIViewAutoresizingFlexibleTopMargin;

    button.frame =CGRectMake(0.0,0.0, buttonImage.size.width, buttonImage.size.height);

    [button setBackgroundImage:buttonImageforState:UIControlStateNormal];

    [button setBackgroundImage:highlightImageforState:UIControlStateHighlighted];

    [button addTarget:selfaction:@selector(didSelectedTabBaritem:)forControlEvents:UIControlEventTouchUpInside];

    

   CGFloat heightDifference = buttonImage.size.height -self.tabBar.frame.size.height;

   if (heightDifference <0)

        button.center =self.tabBar.center;

   else

    {

       CGPoint center =self.tabBar.center;

        center.y = center.y - heightDifference/2.0;

        button.center = center;

    }

    

    [self.viewaddSubview:button];

}



- (void)addLeftButtonWithImage:(UIImage *)buttonImage

                highlightImage:(UIImage *)highlightImage{

    

    UIButton* button = [UIButtonbuttonWithType:UIButtonTypeCustom];

    button.tag =kBtnLeftTag;

    [button addTarget:selfaction:@selector(didSelectedTabBaritem:)forControlEvents:UIControlEventTouchUpInside];

    button.autoresizingMask =UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin |UIViewAutoresizingFlexibleTopMargin;

   float leftBtn_x =0.0f;

   float leftBtn_y =self.tabBar.frame.origin.y;

    button.frame =CGRectMake(leftBtn_x, leftBtn_y,btnWidth,btnHeight);

    [button setBackgroundImage:buttonImageforState:UIControlStateNormal];

    [button setBackgroundImage:highlightImageforState:UIControlStateHighlighted];


    [self.viewaddSubview:button];

}


- (void)addRightButtonWithImage:(UIImage *)buttonImage

                 highlightImage:(UIImage *)highlightImage{

    

    UIButton* button = [UIButtonbuttonWithType:UIButtonTypeCustom];

    button.tag =kBtnRightTag;

    [button addTarget:selfaction:@selector(didSelectedTabBaritem:)forControlEvents:UIControlEventTouchUpInside];

    button.autoresizingMask =UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin |UIViewAutoresizingFlexibleTopMargin;

   float leftBtn_x =self.tabBar.frame.size.width -btnWidth;

   float leftBtn_y =self.tabBar.frame.origin.y;

    button.frame =CGRectMake(leftBtn_x, leftBtn_y,btnWidth,btnHeight);

    [button setBackgroundImage:buttonImageforState:UIControlStateNormal];

    [button setBackgroundImage:highlightImageforState:UIControlStateHighlighted];

    

    [self.viewaddSubview:button];

    

}


- (void)didSelectedTabBaritem:(UIButton *)btn{

    self.selectedIndex = btn.tag;

}



适用于较少的Tabbaritem且中间突出的,效果图





原创粉丝点击