UITabBarController的创建与自定义TarBar---学习笔记三
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #import <UIKit/UIKit.h> @ interface BSJTabBarViewController : UITabBarController @end #import "BSJTabBarViewController.h" #import "BSJTabBar.h" @ interface BSJTabBarViewController () @end @implementation BSJTabBarViewController #pragma mark --- life cycle - ( void )viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // self.tabBarItem = [UITabBarItem application] UITabBarItem *tabBarItem = [UITabBarItem appearance]; //未选中的 NSMutableDictionary *norAttri = [NSMutableDictionary dictionary]; norAttri[NSFontAttributeName] = [UIFont systemFontOfSize:14]; norAttri[NSForegroundColorAttributeName] = [UIColor grayColor]; [tabBarItem setTitleTextAttributes:norAttri forState:UIControlStateNormal]; //选中的 NSMutableDictionary *selectAttri = [NSMutableDictionary dictionary]; selectAttri[NSForegroundColorAttributeName] = [UIColor darkGrayColor]; [tabBarItem setTitleTextAttributes:selectAttri forState:UIControlStateSelected]; //精华 [self setupChridViewController:[[UITableViewController alloc] init] title:NSLocalizedString( @"精华" , nil) normalImage: @"tabBar_essence_icon" selectedImage: @"tabBar_essence_click_icon" ]; [self setupChridViewController:[[UIViewController alloc] init] title:NSLocalizedString( @"关注" , nil) normalImage: @"tabBar_friendTrends_icon" selectedImage: @"tabBar_friendTrends_click_icon" ]; [self setupChridViewController:[[UITableViewController alloc] init] title:NSLocalizedString( @"新帖" , nil) normalImage: @"tabBar_new_icon" selectedImage: @"tabBar_new_click_icon" ]; [self setupChridViewController:[[UITableViewController alloc] init] title:NSLocalizedString( @"我" , nil) normalImage: @"tabBar_me_icon" selectedImage: @"tabBar_me_click_icon" ]; // 通过KVC,可以改变属性为readOnly,改变它的Key,也就是通过它的value找到它的key [self setValue:[[BSJTabBar alloc] init] forKey: @"tabBar" ]; } - ( void )setupChridViewController:(UIViewController *)vc title:(NSString *)title normalImage:(NSString *)normalImage selectedImage:(NSString *)selectedImage{ vc.title = title; if (normalImage.length && selectedImage.length ) { vc.tabBarItem.image = [UIImage imageNamed:normalImage]; vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage]; } [self addChildViewController:vc]; } //tabBar #import <UIKit/UIKit.h> @ interface BSJTabBar : UITabBar @end #import "BSJTabBar.h" @ interface BSJTabBar() @property (nonatomic, weak)UIButton *publicButton; @end @implementation BSJTabBar - ( void )layoutSubviews{ [super layoutSubviews]; // BSJLogFunc CGFloat buttonW = self.frame.size.width/5; CGFloat buttonH = self.frame.size.height; CGFloat buttonY = 0; CGFloat i = 0; for (UIView *subView in self.subviews) { BSJLog( @"%@" ,NSStringFromClass(subView. class )); // if (![subView.class isSubclassOfClass:[NSClassFromString(@"UITabBarButton") class]]) continue; if (![ @"UITabBarButton" isEqualToString:NSStringFromClass(subView. class )]) continue ; CGFloat buttonX = i*buttonW; if (i >= 2) { buttonX += buttonW; } subView.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH); i++; } // [ self.publicButton setFrame:CGRectMake(0, 0, self.frame.size.width/5, self.frame.size.height)]; // self.publicButton.center = CGPointMake(self.center.x, self.center.y); } #pragma mark --- response methods - ( void )buttonClick:(UIButton *)sender{ } #pragma mark --- setters and getters - (UIButton *)publicButton{ if (!_publicButton) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed: @"tabBar_publish_icon" ] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed: @"tabBar_publish_click_icon" ] forState:UIControlStateHighlighted]; [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; _publicButton = button; } return _publicButton; } @end |
将来的自己,会感谢现在不放弃的自己!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现