UI_自定义标签控制器

#import "AppDelegate.h"

#import "RootTabBarController.h"



@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    

    

    RootTabBarController *root = [RootTabBarController new];

    self.window.rootViewController = root;

    

    

    

    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}

@end





#import "RootTabBarController.h"

#import "OneViewController.h"

#import "TwoViewController.h"

#import "CustomTabBarView.h"



@interface RootTabBarController ()<CustomTabBarViewDelegate>


@end


@implementation RootTabBarController


- (void)viewDidLoad {

    [super viewDidLoad];


    OneViewController *oneVC = [OneViewController new];

    UINavigationController *oneNavC = [[UINavigationControlleralloc]initWithRootViewController:oneVC];

    

    

    TwoViewController *twoVC = [TwoViewController new];

    UINavigationController *twoNavC = [[UINavigationControlleralloc]initWithRootViewController:twoVC];

    

    

    

    //tabVarController添加子控制器(等同于:将标签加入到控制器中)

    [self addChildViewController:oneNavC];

    [self addChildViewController:twoNavC];

    

      

    

//自定义

    //如果使用自定义的tabBar,可以先将系统的隐藏掉,然后用自己的

    self.tabBar.hidden = YES;

    

    //初始化自定义的tabBar

    CustomTabBarView *customView = [[CustomTabBarViewalloc]initWithFrame:CGRectZero];

    //指定代理

    customView.delegate = self;

    

    [self.view addSubview:customView];

    

    self.view.backgroundColor = [UIColor yellowColor];

 

    

}


//实现代理方法

-(void)selectIndexWithTag:(int)tag{

    self.selectedIndex = tag;


}








- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end



#import "OneViewController.h"


@interface OneViewController ()


@end


@implementation OneViewController


- (void)viewDidLoad {

    [super viewDidLoad];

 self.navigationItem.title = @"One";

 self.view.backgroundColor = [UIColor cyanColor];

    

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}




#import "TwoViewController.h"


@interface TwoViewController ()


@end


@implementation TwoViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.title = @"Two";

    self.view.backgroundColor = [UIColor grayColor];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#import "CustomTabBarView.h"


@implementation CustomTabBarView


//初始化界面元素

-(void)createSubViews{

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

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

        [btn setTitle:[@(i) stringValue] forState:UIControlStateNormal];

        [btn setTitle:@"MLGBWC" forState:UIControlStateSelected];

            //通过tag值来确定我们点击的是哪个按钮,来切换到对应的视图控制器

        [btn setTag:1000+i];

        [btn addTarget:self action:@selector(customSelectorIndex:) forControlEvents:UIControlEventTouchDown];

        //设置按钮的位置

        btn.frame = CGRectMake((kScreenWidth/2*i), 0, kScreenWidth/2, 50);

        //添加按钮

        [self addSubview:btn];

    }

}


//按钮的回调方法

-(void)customSelectorIndex:(UIButton *)sender{

        //这个方法的作用是:判断在指定的代理类中是否实现了该协议方法,如果实现,返回值为YES,如果未实现该协议方法,返回值为NO,这样就保证了执行协议方法的时候不会崩溃。

    if ([self.delegate respondsToSelector:@selector(selectIndexWithTag:)]) {

        [self.delegate selectIndexWithTag:(int)sender.tag-1000];

    }else{

        NSLog(@"selectIndexWithTag--该方法未实现");

    }

    

        //给按钮添加选中状态

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

        UIButton *btn = (UIButton *)[self viewWithTag:1000+i];

        btn.selected = NO;

    }

        sender.selected = YES;

    

   

    

    NSLog(@"按钮回调方法");

}







//写初始化方法

-(instancetype)initWithFrame:(CGRect)frame{

    //重新设置frame,对它进行一个保护

    frame = CGRectMake(0, kScreenHeight-50, kScreenWidth, 50);

    

    self = [super initWithFrame:frame];

    if (self) {

        [self createSubViews];

            //更改颜色

        self.backgroundColor = [UIColor yellowColor];

    }

    return self;

}



@end

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值