iOS--自定义UITabBarController


//

//  MainTabBarController.m

//  UITabBarControllerCustomDemo

//

//  Created by LiZe on 13-8-29.

//  Copyright (c) 2013 BlackCode. All rights reserved.

//


#import "MainTabBarController.h"


@interface MainTabBarController () {

    float imageHeightY; // 所有图片距离y轴的位置

    UILabel *happyLabel; // 开心标签

    UILabel *quietLabel; // 安静

    UILabel *sadLabel; // 伤心

    UILabel *exctingLabel; // 兴奋

    UILabel *missLabel; // 想念

}



#pragma mark - 声明--加载全部UIViewController的方法

- (void)_loadViewAllController;


#pragma mark - 声明--加载CustomUITabBarController的方法

- (void)_loadViewCustomUITabBarController;


@end


@implementation MainTabBarController


#pragma mark - 实现----加载全部UIViewController的方法

- (void)_loadViewAllController {

    // 添加日记视图

    HappyViewController *happyVC = [[HappyViewController alloc] init];

    QuietViewController *quietVC = [[QuietViewController alloc] init];

    SadViewController *sadVC = [[SadViewController alloc] init];

    ExctingViewController *exctingVC = [[ExctingViewController alloc] init];

    MissViewController *missVC = [[MissViewController alloc] init];

    

    // 放入到数组中

    NSArray *viewController = @[happyVC, quietVC, sadVC, exctingVC,missVC];

    [happyVC release], happyVC = nil;

    [quietVC release], quietVC = nil;

    [sadVC release], sadVC = nil;

    [exctingVC release], exctingVC = nil;

    [missVC release], missVC = nil;

                     

    

    // 添加到当前ViewTabBarController上面

    [self setViewControllers:viewController animated:YES];

}


#pragma mark - 实现----加载CustomUITabBarController的方法

- (void)_loadViewCustomUITabBarController {

   

    

    // 所有图片距离y轴的位置

    imageHeightY = self.view.frame.size.height - 60;

    NSLog(@"%f", imageHeightY);

    

    // 设置背景图片

    _tabBarBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0,imageHeightY, 320, 60)];

    _tabBarBackground.image = [UIImage imageNamed:@"tab_bar_background"];

    _tabBarBackground.userInteractionEnabled = YES // 一定要设置为可以与用户交互

    [self.view addSubview:_tabBarBackground];

    

    

        

    

    // 设置上面的UITabBarItem

    // 开心

    UIButton *happyButton = [UIButton buttonWithType:UIButtonTypeCustom];

    happyButton.frame = CGRectMake(-18, -1010876);

    happyButton.tag = 0;

    [_tabBarBackground addSubview:happyButton];

    

    

    

    

    // 安静

    UIButton *quietButton = [UIButton buttonWithType:UIButtonTypeCustom];

    quietButton.frame = CGRectMake(44, -1010876);

    quietButton.tag = 1;

    [_tabBarBackground addSubview:quietButton];

    

    

    

    

    // 伤心

    UIButton *sadEassyButton = [UIButton buttonWithType:UIButtonTypeCustom];

    sadEassyButton.frame = CGRectMake(105, -1010876);

    sadEassyButton.tag = 2;

    [_tabBarBackground addSubview:sadEassyButton];

   

    

    


    // 兴奋

    UIButton *exctingEassyButton = [UIButton buttonWithType:UIButtonTypeCustom];

    exctingEassyButton.frame = CGRectMake(166, -1010876);

    exctingEassyButton.tag = 3;

    [_tabBarBackground addSubview:exctingEassyButton];

    

    

    

    

    // 想念

    UIButton *missEassyButton = [UIButton buttonWithType:UIButtonTypeCustom];

    missEassyButton.frame = CGRectMake(227, -1010876);

    missEassyButton.tag = 4;

    [_tabBarBackground addSubview:missEassyButton];

    

    


    // 快速遍历设置UIButton

    for (UIButton *button in _tabBarBackground.subviews) {

        if ([button isKindOfClass:[UIButton class]]) {

            [button setImage:[UIImage imageNamed:@"tab_bar_normal"]forState:UIControlStateNormal];

            [button addTarget:self action:@selector(tabBarItemChangeSelected:)forControlEvents:UIControlEventTouchUpInside]; // 添加监听器

        }

    }

    

    

    // 设置被选中的图片

    _tabBarSelectedImage = [[UIImageView alloc] initWithFrame:CGRectMake(-18, -10, 108, 76)];

    _tabBarSelectedImage.image = [UIImage imageNamed:@"tab_bar_select"];

    [_tabBarBackground addSubview:_tabBarSelectedImage]; // 添加到当前的背景上

    


    // 开心标签

    happyLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 14, 50, 30)];

    happyLabel.text = @"开心";

    [_tabBarBackground insertSubview:happyLabelaboveSubview:_tabBarSelectedImage];

    [happyLabel release], happyLabel = nil;

    

    // 安静

    quietLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 13, 50, 30)];

    quietLabel.text = @"安静";

    [_tabBarBackground addSubview:quietLabel];

    [quietLabel release], quietLabel = nil;

    

    // 伤心

    sadLabel = [[UILabel alloc] initWithFrame:CGRectMake(143, 14, 50, 30)];

    sadLabel.text = @"伤心";

    [_tabBarBackground addSubview:sadLabel];

    [sadLabel release], sadLabel = nil;

    

    // 兴奋

    exctingLabel = [[UILabel alloc] initWithFrame:CGRectMake(203, 14, 50, 30)];

    exctingLabel.text = @"兴奋";

    [_tabBarBackground addSubview:exctingLabel];

    [exctingLabel release], exctingLabel = nil;

    

    // 想念

    missLabel = [[UILabel alloc] initWithFrame:CGRectMake(263, 14, 50, 30)];

    missLabel.text = @"想念";

    [_tabBarBackground addSubview:missLabel];

    [missLabel release], missLabel = nil;

    

    // 快速遍历设置UILabel

    for (UILabel *label in _tabBarBackground.subviews) {

        if ([label isKindOfClass:[UILabel class]]) {

            label.backgroundColor = [UIColor clearColor];

            label.textColor = [UIColor brownColor];

            label.font = [UIFont boldSystemFontOfSize:18.0f];

        }

    }


    

}



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

        // 隐藏TabBarController

        self.tabBar.hidden = YES;

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

    // 调用方法,加载全部页面

    [self _loadViewAllController];

    

    // 调用方法,自定义TabBarController

    [self _loadViewCustomUITabBarController];

    

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark - 实现----tabBarItem选中的方法

- (void)tabBarItemChangeSelected:(UIButton *) button {

    self.selectedIndex = button.tag;

    // 设置动画效果

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:0.5];

    switch (button.tag) {

        case 0:

            _tabBarSelectedImage.frame = CGRectMake(-18, -1010876);

            break;

        case 1:

            _tabBarSelectedImage.frame = CGRectMake(44, -1010876);

            break;

        case 2:

            _tabBarSelectedImage.frame = CGRectMake(105, -1010876);

            break;

        case 3:

            _tabBarSelectedImage.frame = CGRectMake(166, -1010876);

            break;

        case 4:

            _tabBarSelectedImage.frame = CGRectMake(227, -1010876);

            break;

        default:

            break;

    }

    [UIView commitAnimations];

}



#pragma mark - 重写dealloc方法

-(void)dealloc {

    [_tabBarBackground release], _tabBarBackground = nil;

    [_tabBarSelectedImage release], _tabBarSelectedImage = nil;

    

    [super dealloc];

}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 中,可以使用 Objective-C 自定义 TabBar。下面是一个简单的步骤: 1. 创建一个继承自 UITabBarController 的类,例如 MyTabBarController。 2. 在 MyTabBarController.m 中,实现 viewDidLoad 方法,在其中创建自定义 TabBar。 ```objc - (void)viewDidLoad { [super viewDidLoad]; // 创建自定义 TabBar MyTabBar *myTabBar = [[MyTabBar alloc] init]; myTabBar.delegate = self; [self setValue:myTabBar forKey:@"tabBar"]; } ``` 3. 在 MyTabBarController.m 中,实现 UITabBarDelegate 协议中的方法,例如 tabBar:didSelectItem:,处理 TabBar 点击事件。 ```objc - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { // 处理 TabBar 点击事件 } ``` 4. 创建一个继承自 UITabBar 的类,例如 MyTabBar。 5. 在 MyTabBar.m 中,实现 layoutSubviews 方法,在其中布局 TabBar。 ```objc - (void)layoutSubviews { [super layoutSubviews]; // 布局 TabBar } ``` 6. 在 MyTabBar.m 中,实现 drawRect: 方法,在其中绘制 TabBar。 ```objc - (void)drawRect:(CGRect)rect { // 绘制 TabBar } ``` 7. 在 MyTabBar.m 中,实现 touchUpInside 方法,在其中处理 TabBar 点击事件。 ```objc - (void)touchUpInside:(UIButton *)button { // 处理 TabBar 点击事件 } ``` 8. 在 MyTabBar.m 中,添加 TabBar 上的按钮,例如: ```objc - (void)layoutSubviews { [super layoutSubviews]; // 添加 TabBar 上的按钮 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; button1.frame = CGRectMake(0, 0, self.frame.size.width / 4, self.frame.size.height); [button1 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button1]; UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom]; button2.frame = CGRectMake(self.frame.size.width / 4, 0, self.frame.size.width / 4, self.frame.size.height); [button2 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button2]; UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom]; button3.frame = CGRectMake(self.frame.size.width / 2, 0, self.frame.size.width / 4, self.frame.size.height); [button3 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button3]; UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom]; button4.frame = CGRectMake(self.frame.size.width * 3 / 4, 0, self.frame.size.width / 4, self.frame.size.height); [button4 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button4]; } ``` 9. 在 MyTabBar.m 中,重写 setItems:animated: 方法,在其中设置 TabBar 上按钮的图标和标题。 ```objc - (void)setItems:(NSArray<UITabBarItem *> *)items animated:(BOOL)animated { [super setItems:items animated:animated]; // 设置 TabBar 上按钮的图标和标题 for (int i = 0; i < items.count; i++) { UITabBarItem *item = items[i]; UIButton *button = self.subviews[i + 1]; [button setImage:item.image forState:UIControlStateNormal]; [button setTitle:item.title forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; } } ``` 以上就是使用 Objective-C 自定义 TabBar 的简单步骤。当然,还可以根据需求进行更详细的定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值