大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

自定义tabBar详解第二种方案-创新互联

自定义tabBar能够解决自定义图片无法显示(只显示一块颜色)这个弊端,自定义tabBar要继承UITabBarController

创新互联是一家专注于成都网站建设、网站建设与策划设计,大渡口网站建设哪家好?创新互联做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:大渡口等地区。大渡口做网站价格咨询:028-86922220

原图片:

(1), 自定义tabBar详解第二种方案 (2), 自定义tabBar详解第二种方案(3), 自定义tabBar详解第二种方案(4), 自定义tabBar详解第二种方案(5)自定义tabBar详解第二种方案

下面是自定义了一个TabBar ,每个按钮可以显示图片(自定义避免了图片无法显示只显示一块颜色)

效果图:

自定义tabBar详解第二种方案

代码实现:

一, 首先创建一个继承UIButton的类CustomTabBar,

在.h文件中:

//指定协议是为了实现点击button显示相应地页面,像系统的tabBar一样

@protocol CustomTabBarDelegate

-(void)didSelectBarItemAtIndex:(NSInteger)index;

@end

@interface CustomTabBar : UIButton
@property(nonatomic,assign) NSInteger index;
@property (nonatomic,retain) id delegate;
- (id)initWithFrame:(CGRect)frame WithImage:(UIImage *)p_w_picpath AndSelectedImage:(UIImage *)p_w_picpath;
@end

在.m文件中

#import "CustomTabBar.h"

@interface CustomTabBar ()

@end
@implementation CustomTabBar

- (id)initWithFrame:(CGRect)frame
{
   self = [super initWithFrame:frame];
   if (self) {
       // Initialization code

   }
   return self;
}

- (id)initWithFrame:(CGRect)frame WithImage:(UIImage *)p_w_picpath AndSelectedImage:(UIImage *)selectedImage{
   self = [super initWithFrame:frame];
   if (self) {
       self.frame = frame;
       [self setAdjustsImageWhenHighlighted:NO];
       [self setImage:p_w_picpath forState:UIControlStateNormal];
       [self setImage:selectedImage forState:UIControlStateSelected];
       [self addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
   }
   return self;
}

-(void)click:(UIButton *)btn{
   if ([self.delegate respondsToSelector:@selector(didSelectBarItemAtIndex:)]) {
       [self.delegate didSelectBarItemAtIndex:self.index];
   }
   switch ((int)(btn.selected)) {
       case 0:
           btn.selected = YES;
           break;
       case 1:
           btn.selected = NO;
           break;
       default:
           break;
   }
}

@end

二, 创建一个类CustomizedTabBarController继承UITabBarController

在.m文件中

- (void)loadViewController
{
   CoverCollectionViewController *coverVC = [[CoverCollectionViewController alloc] init];
   //coverVC.title = @"每日封面";
   CustomizedNavigationController *coverNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:coverVC];
   coverNaVC.navigationItem.title = @"iDailyWATCh";

   NewsTableViewController *newsVC = [[NewsTableViewController alloc] init];
   //newsVC.title = @"腕表杂志";
   CustomizedNavigationController *newsNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:newsVC];

   BrandsTableViewController *brandsVC = [[BrandsTableViewController alloc] init];
   //brandsVC.title = @"品牌维基";
   CustomizedNavigationController *brandsNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:brandsVC];

   VideosCollectionViewController *videosVC = [[VideosCollectionViewController alloc] init];
   //videosVC.title = @"视频";
   CustomizedNavigationController *videosNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:videosVC];

   MoreViewController *moreVC = [[MoreViewController alloc] init];
   //moreVC.title = @"更多";
   CustomizedNavigationController *moreNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:moreVC];

   self.viewControllers = @[coverNaVC, newsNaVC, brandsNaVC, videosNaVC, moreNaVC];
   self.selectedIndex = 0;
   RELEASE_SAFE(coverNaVC);
   RELEASE_SAFE(coverVC);
   RELEASE_SAFE(brandsNaVC);
   RELEASE_SAFE(brandsVC);
   RELEASE_SAFE(videosNaVC);
   RELEASE_SAFE(videosVC);
   RELEASE_SAFE(moreNaVC);
   RELEASE_SAFE(moreVC);
}

- (void)setupTabBar
{
   for (int i = 0 ; i < 5; i++) {
       if (i < 3) {
            CustomTabBar *bar = [[CustomTabBar alloc]initWithFrame:CGRectMake(0 + 80 * i, 0, 80, 49) WithImage: [UIImage p_w_picpathNamed:[NSString stringWithFormat:@"%d", i + 100]]AndSelectedImage:[UIImage p_w_picpathNamed:[NSString stringWithFormat:@"%d", i + 130]]];
           bar.index = i;
           bar.delegate = self;
           bar.backgroundColor = RGB(245, 245, 245);
           [self.tabBar addSubview:bar];
           [bar release];
           if (i == 0) {
               bar.selected = YES;
           }
       } else {
            CustomTabBar *bar = [[CustomTabBar alloc]initWithFrame:CGRectMake(0 + 40 * (i + 3), 0, 40, 49) WithImage: [UIImage p_w_picpathNamed:[NSString stringWithFormat:@"%d", i + 100]]AndSelectedImage:[UIImage p_w_picpathNamed:[NSString stringWithFormat:@"%d", i + 130]]];
           bar.index = i;
           bar.delegate = self;
           bar.backgroundColor = RGB(245, 245, 245);
           [self.tabBar addSubview:bar];
           [bar release];
       }
   }
}

-(void)didSelectBarItemAtIndex:(NSInteger)index{
   [self resignBatState];
   self.selectedViewController = self.viewControllers[index];
}
-(void)resignBatState{
   NSArray *arr = [self.tabBar subviews];
   for (CustomTabBar *bar in arr) {
       if ([bar isKindOfClass:[CustomTabBar class]]) {
           bar.selected = NO;
       }
   }
}

- (void)changeViewController:(UIButton *)button
{
   self.selectedIndex = button.tag;
}

- (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:自定义tabBar详解第二种方案-创新互联
链接分享:http://dzwzjz.com/article/jppcj.html
在线咨询
服务热线
服务热线:028-86922220
TOP