【深入浅出IOS开发】1.面搭建-彩票

实现如下功能:

①自定义TabBarController中的TabBar

②点击TabBar中的按钮会变高亮,其他的正常。

③点击TabBar中的按钮,里面的子控制器切换

如图:


头文件

#import <UIKit/UIKit.h>

@interface MRTabBarViewController : UITabBarController
@property (nonatomic,weak) UIButton* button;
@end

实现文件:

//
//  MRTabBarViewController.m
//  lotteries
//
//  Created by Asuna on 14/12/26.
//  Copyright (c) 2014年 Asuna. All rights reserved.
//

#import "MRTabBarViewController.h"

@implementation MRTabBarViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    //1.清除原有的TabBar
    [self.tabBar removeFromSuperview];
    //2.生成一个新的TabBar
    UITabBar *mrTabBar = [[UITabBar alloc]initWithFrame:self.tabBar.frame];
    [mrTabBar setBackgroundColor:[UIColor redColor]];
    //3.将Tabbar添加到View中
    [self.view addSubview:mrTabBar];
    
    for (int i = 0; i < 5; i++) {
        //1.创建一个按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        //2.设置按钮正常显示的图片
        NSString *strImage = [NSString stringWithFormat:@"TabBar%d",i+1];
        UIImage *image = [UIImage imageNamed:strImage];
        [button setImage:image forState:UIControlStateNormal];
        //3.设置按钮被选中的时候显示的图片
        NSString *strImageSel = [NSString stringWithFormat:@"TabBar%dSel",i+1];
        UIImage *imagehighLSel = [UIImage imageNamed:strImageSel];
        [button setImage:imagehighLSel forState:UIControlStateSelected];
        //4.设置按钮的frame
        CGFloat width = mrTabBar.frame.size.width*0.2;
        CGFloat height = mrTabBar.frame.size.height;
        CGFloat xPos = i*width;
        CGFloat yPos = 0;
        
        [button setFrame:CGRectMake(xPos, yPos, width, height)];
        //5.设置按钮的Target
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        //6.设置按钮的tag
        [button setTag:i];
        //7.添加按钮到Tabbar
        [mrTabBar addSubview:button];
        
        if (i == 0) {
            [self click:button];
        }
    }
    
}

-(void)click:(UIButton*)button
{
    <pre name="code" class="objc">    //1.设置当前按钮不被选中
    self.button.selected =NO;
 
    //2.新点击的按钮被选中
    button.selected = YES;

    //3.把新点击的按钮设置为当前按钮
    self.button = button;
    //4.切换子控制器
    self.selectedIndex = button.tag;
    
}
@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值