UITabBar的简单使用

本文介绍如何在 iOS 应用中手动创建并配置 UITabBar 控件,包括设置项目标题、图标、选中状态图像及徽章值等。同时展示了如何通过委托方法响应用户的点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.h文件中实现<UITabBarDelegate>


.m文件

-(void)viewDidLoad

{

UITabBar *tab1 = [[UITabBar alloc] initWithFrame:CGRectMake(0, 100, 320, 50)];

    

    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"Item1" image:nil tag:1];

    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"Item2" image:nil tag:2];

    UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"Item3" image:nil tag:3];

    UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:@"Item4" image:nil tag:4];

    

    tab1.delegate = self;

tan

    item4.badgeValue = @"3"//文档上说是文本右上角的椭圆显示的文本,但是显示实在左上角

    item4.selectedImage = [UIImage imageNamed:@"pp_hzdc_tp_06"];     //设置被选择时的图片(好像不管用)

    

    NSArray *array = [[NSArray alloc] initWithObjects:item1,item2,item3,item4, nil];

    [tab1 setItems:array animated:YES];

    [self.view addSubview:tab1];

}


//实现delegate的方法,可以用作item的点击响应

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

    NSLog(@"item.tag is %d",item.tag);

    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值