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

来源:互联网 发布:户撒刀王项老赛淘宝店 编辑:程序博客网 时间:2024/05/20 02:22

实现如下功能:

①自定义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
原创粉丝点击