簡單創建標籤欄

//  AppDelegate.m

#import "AppDelegate.h"
#import "WHViewController.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    WHViewController *tabBarcont1 = [[WHViewController alloc] init];
    self.window.rootViewController = tabBarcont1;
    [self.window makeKeyAndVisible];
    
    return YES;
}
	
//  WHViewController.m

#import "WHViewController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"

@interface WHViewController ()
{
    NSMutableArray *_tempAry;
}

@end

@implementation WHViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _tempAry = [NSMutableArray array];
    FourViewController *vc4 = [FourViewController new];
    OneViewController *vc1 = [OneViewController new];
    TwoViewController *vc2 = [TwoViewController new];
    ThreeViewController *vc3 = [ThreeViewController new];
    
    self.viewControllers = @[vc1,vc2,vc3,vc4];
    
    //自定義標籤背景
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"tabbg.png"];
    imageView.frame = CGRectMake(0, self.view.frame.size.height-49, 320, 49);
    [self.view addSubview:imageView];
    imageView.userInteractionEnabled = YES;
   
    //添加標籤欄上得按鈕
    float spaceX = (self.view.frame.size.width - 4*30)/5 ;
    for (int i = 0; i <4; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i+11;
        btn.frame = CGRectMake(spaceX*(i+1)+30*i, (49-30)/2, 30, 30);
        UIImage *imageNomal = [UIImage imageNamed:[NSString stringWithFormat:@"tabBtn%i_1.png",i+1]];
        UIImage *imageSelect = [UIImage imageNamed:[NSString stringWithFormat:@"tabBtn%i_2.png",i+1]];
        [btn setImage:imageNomal forState:UIControlStateNormal];
        [btn setImage:imageSelect forState:UIControlStateSelected];
        //
        [imageView addSubview:btn];
        [_tempAry addObject:btn];
        if (btn.tag == 11) {
            btn.selected =YES ;
        }
        //點擊按鈕,進入點擊方法
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}
- (void)clickBtn:(UIButton*)btn
{
    for (UIButton *tempBtn in _tempAry) {
        if (tempBtn.tag == btn.tag) {
            tempBtn.selected = YES;
        }else{
            tempBtn.selected = NO;
        }
    }
    
    
    int index = btn.tag - 11;
    self.selectedIndex = index;
}

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


@end

發佈了52 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章