ITeye博客

自定义uitarbarItem

修博龙泉 · 2012-10-23 15:36 · ios开发学习相关 · 移动开发 · Markdown

-(void)viewDidAppear:(BOOL)animated{

[self showBadgeValue:@"10"];

}

  • (void)showBadgeValue:(NSString *)strBadgeValue { UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,100, 100)]; [imageView setImage:[UIImage imageNamed:@"background.png"]]; //[self.view addSubview:imageView]; UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"jjj" image:imageView.image tag:0]; item.badgeValue = strBadgeValue;

    NSArray *array = [[NSArray alloc] initWithObjects:item, nil]; tabBar.items = array; [self.view addSubview:tabBar]; [item release]; [array release];

    //寻找 // for (UIView *viewTab in tabBar.subviews) { // for (UIView *subview in viewTab.subviews) { // NSString *strClassName = [NSString stringWithUTF8String:object_getClassName(subview)]; // if ([strClassName isEqualToString:@"UITabBarButtonBadge"] || // [strClassName isEqualToString:@"_UIBadgeView"]) { // //从原视图上移除 // [subview removeFromSuperview]; // // // [self.view addSubview:subview]; // subview.frame = CGRectMake(self.frame.size.width-subview.frame.size.width, 0, // subview.frame.size.width, subview.frame.size.height); // [tabBar release]; // return subview; // } // } // } // [tabBar release];

}

  • (void)removeBadgeValue { // for (UIView *subview in self.view.subviews) { NSString *strClassName = [NSString stringWithUTF8String:object_getClassName(subview)]; if ([strClassName isEqualToString:@"UITabBarButtonBadge"] || [strClassName isEqualToString:@"_UIBadgeView"]) { [subview removeFromSuperview]; break; } } }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。