UITabBar初体验

来源:互联网 发布:舆论监督与网络暴力 编辑:程序博客网 时间:2024/05/17 12:54

#import "ViewControllerTwo.h"


@interface ViewControllerTwo ()


@end


@implementation ViewControllerTwo

@synthesize tabBar;


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

{

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

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

self.view.backgroundColor = [UIColor greenColor];

    tabBar = [[UITabBar alloc] init];

    tabBar.delegate = self;

    tabBar.frame = CGRectMake(0, 50, 320, 50);

    tabBar.tintColor = [UIColor colorWithRed:193.0f/255.0f green:193.0f/255.0f blue:193.0f/255.0f alpha:1.0];

    UITabBarItem * tb1 = [[UITabBarItem alloc] initWithTitle:@"1" image:nil tag:101];

    [tb1 setTitlePositionAdjustment:UIOffsetMake(0.0, -4.0)];

    UIOffset offset = [tb1 titlePositionAdjustment];

    NSLog(@"%@",NSStringFromUIOffset(offset));

    tb1.badgeValue = @"2";

    UITabBarItem * tb2 = [[UITabBarItem alloc] initWithTitle:@"2" image:nil tag:102];

    NSArray * array = [NSArray arrayWithObjects:tb1,tb2, nil];

    tabBar.items = array;

    [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];

    [self addBtn];

    [self.view addSubview:tabBar];

}


#pragma mark -


- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

{

    if(item.tag == 101)

    {

        [self addBtn];

    }

    else

    {

        [self addLabel];

    }

}


- (void)addBtn

{

    for (UIView * v in self.view.subviews)

    {

        if (v.tag == 151)

        {

            [v removeFromSuperview];

        }

    }

    UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(150, 200, 50, 50);

    btn.tag = 150;

    [self.view addSubview:btn];

}


-(void)addLabel

{

    for (UIView * v in self.view.subviews)

    {

        if(v.tag == 150)

        {

            [v removeFromSuperview];

        }

    }

    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 50, 50)];

    label.tag = 151;

    label.backgroundColor = [UIColor blueColor];

    [self.view addSubview:label];

    [label release];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



原创粉丝点击