【浅学封装思想】
实例壹:创建微博框架。首先是创建一个UITabBarController,在下面显示栏再创建四个 UIViewController。如下四个文本:
1 // AppDelegate.h - 版本1 2 // Emo微博 3 4 #import <UIKit/UIKit.h> 5 6 @interface AppDelegate : UIResponder <UIApplicationDelegate> 7 @property (strong, nonatomic) UIWindow *window; 8 @end
1 // AppDelegate.m - 版本1 2 // Emo微博 3 4 #import "AppDelegate.h" 5 #import "EmoTabBarController.h" 6 // 封装思想:如果以后项目中,有相同的功能,抽取一个类,封装好,如何封装,自己的事情全部交给自己管理。 7 // 抽方法:一般一个功能就抽一个方法。 8 9 @interface AppDelegate () 10 @end 11 12 @implementation AppDelegate 13 14 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 15 // Override point for customization after application launch. 16 17 // 创建窗口 18 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 19 // 创建tabBarVc 20 EmoTabBarController *tabBarVc = [[EmoTabBarController alloc] init]; 21 // 设置窗口的根控制器 22 self.window.rootViewController = tabBarVc; 23 // 显示窗口 24 [self.window makeKeyAndVisible]; 25 26 return YES; 27 } 28 @end
1 // EmoTabBarController.h - 版本1 2 // Emo微博 3 4 #import <UIKit/UIKit.h> 5 6 @interface EmoTabBarController : UITabBarController 7 @end
1 // EmoTabBarController.m - 版本1 2 // Emo微博 3 #import "EmoTabBarController.h" 4 5 @interface EmoTabBarController() 6 @end 7 8 @implementation EmoTabBarController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 [self setUpAllChildViewController]; 13 } 14 15 - (void)setUpAllChildViewController 16 { 17 18 // 首页 19 UIViewController *home = [[UIViewController alloc] init]; 20 home.tabBarItem.title = @"首页"; 21 home.tabBarItem.image = [UIImage imageNamed:@"tabbar_home"]; 22 // home.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_home_selected"];//仅仅是这句话那么图片控件会被系统默认为是蓝色。 23 UIImage*selImage = [UIImage imageNamed:@"tabbar_home_selected"]; 24 selImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 25 home.tabBarItem.selectedImage =selImage; 26 home.view.backgroundColor = [UIColor greenColor]; 27 [self addChildViewController:home]; 28 29 // 消息 30 UIViewController *message = [[UIViewController alloc] init]; 31 message.tabBarItem.title = @"消息"; 32 message.view.backgroundColor = [UIColor blueColor]; 33 message.tabBarItem.image = [UIImage imageNamed:@"tabbar_message"]; 34 UIImage*selImage = [UIImage imageNamed:@"tabbar_message_selected"]; 35 selImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 36 home.tabBarItem.selectedImage =selImage; 37 [self addChildViewController:home]; 38 39 // 发现 40 UIViewController *discover= [[UIViewController alloc] init]; 41 discover.tabBarItem.title = @"发现"; 42 discover.view.backgroundColor = [UIColor redColor]; 43 discover.tabBarItem.image = [UIImage imageNamed:@"tabbar_discover"]; 44 UIImage*selImage = [UIImage imageNamed:@"tabbar_discover_selected"]; 45 selImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 46 discover.tabBarItem.selectedImage =selImage; 47 [self addChildViewController:home]; 48 49 // 我 50 UIViewController *profile= [[UIViewController alloc] init]; 51 profile.tabBarItem.title = @"我"; 52 profile.view.backgroundColor = [UIColor blueColor]; 53 profile.tabBarItem.image = [UIImage imageNamed:@"tabbar_profile"]; 54 UIImage*selImage = [UIImage imageNamed:@"tabbar_profile_selected"]; 55 selImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 56 discover.tabBarItem.selectedImage =selImage; 57 [self addChildViewController:home]; 58 59 }60 @end
实例贰:倘若把下面这一段单独抽取出来写成分类,
1 // EmoTabBarController.m 2 // Emo微博...
23 UIImage*selImage = [UIImage imageNamed:@"tabbar_home_selected"]; 24 selImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 25 home.tabBarItem.selectedImage =selImage;
可以先创建一个分类“UIImage+Image”
1 // UIImage+Image.h - 版本1 2 // Emo微博 3 4 #import <UIKit/UIKit.h> 5 6 @interface UIImage (Image) 7 + (instancetype)imageWithOriginalName:(NSString *)imageName; 8 @end
1 // UIImage+Image.m - 版本1 2 // Emo微博 3 4 #import "UIImage+Image.h" 5 @implementation UIImage (Image) 6 7 // 抽取出来后改写为“+ (instancetype)imageWithOriginalName:(NSString *)imageName” 8 // UIImage*selImage = [UIImage imageNamed:@"tabbar_home_selected"]; 9 // selImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 10 // home.tabBarItem.selectedImage =selImage; 11 12 + (instancetype)imageWithOriginalName:(NSString *)imageName 13 { 14 UIImage *image = [UIImage imageNamed:imageName]; 15 return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 16 } 17 18 @end
在“EmoTabBarController.m”中抽取出来后,替换上面的” EmoTabBarController.m“为如下代码:
1 // EmoTabBarController.m - 版本2 2 // Emo微博 3 4 #import "EmoTabBarController.h" 5 #import "UIImage+Image.h" 6 7 @interface EmoTabBarController () 8 @end 9 10 @implementation EmoTabBarController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 // Do any additional setup after loading the view. 15 16 // 添加所有子控制器 17 [self setUpAllChildViewController]; 18 19 } 20 // 在ios7之后,默认会把UITabBar上面的按钮图片渲染成蓝色 21 #pragma mark - 添加所有的子控制器 22 - (void)setUpAllChildViewController 23 { 24 // 首页 25 UIViewController *home = [[UIViewController alloc] init]; 26 home.tabBarItem.title = @"首页"; 27 home.tabBarItem.image = [UIImage imageNamed:@"tabbar_home"]; 28 // 调用子类"UImage+image“的方法”imageWithOriginalName“并传入参数 29 home.tabBarItem.selectedImage = [UIImage imageWithOriginalName:@"tabbar_home_selected"]; 30 home.view.backgroundColor = [UIColor greenColor]; 31 [self addChildViewController:home]; 32 33 // 消息 34 UIViewController *message = [[UIViewController alloc] init]; 35 message.tabBarItem.title = @"首页"; 36 message.tabBarItem.image = [UIImage imageNamed:@"tabbar_message"]; 37 message.tabBarItem.selectedImage = [UIImage imageWithOriginalName:@"tabbar_message_selected"]; 38 message.view.backgroundColor = [UIColor greenColor]; 39 [self addChildViewController:message]; 40 41 // 发现 42 UIViewController *discover = [[UIViewController alloc] init]; 43 discover.tabBarItem.title = @"首页"; 44 discover.tabBarItem.image = [UIImage imageNamed:@"tabbar_discover"]; 45 discover.tabBarItem.selectedImage = [UIImage imageWithOriginalName:@"tabbar_discover_selected"]; 46 discoverview.backgroundColor = [UIColor greenColor]; 47 [self addChildViewController:discover]; 48 49 // 我 50 UIViewController *profile = [[UIViewController alloc] init]; 51 profile.tabBarItem.title = @"首页"; 52 profile.tabBarItem.image = [UIImage imageNamed:@"tabbar_profile"]; 53 profile.tabBarItem.selectedImage = [UIImage imageWithOriginalName:@"tabbar_profile_selected"]; 54 profile.view.backgroundColor = [UIColor greenColor]; 55 [self addChildViewController:profile ]; 56 } 57 58 @end
实例叁:再看看下面这一部分代码:
24 // 首页 25 UIViewController *home = [[UIViewController alloc] init]; 26 home.tabBarItem.title = @"首页"; 27 home.tabBarItem.image = [UIImage imageNamed:@"tabbar_home"]; 28 // 调用子类"UImage+image“的方法”imageWithOriginalName“并传入参数 29 home.tabBarItem.selectedImage = [UIImage imageWithOriginalName:@"tabbar_home_selected"]; 30 home.view.backgroundColor = [UIColor greenColor]; 31 [self addChildViewController:home];
其实都是在添加一个子控制器,所以可以抽取出一个方法且加入参数让其传进来,所以代码可以修改为:
1 // EmoTabBarController.m - 版本3 2 // 传智微博 3 4 5 #import "EmoTabBarController.h" 6 #import "UIImage+Image.h" 7 8 @interface EmoTabBarController () 9 @end 10 11 @implementation EmoTabBarController 12 13 - (void)viewDidLoad { 14 [super viewDidLoad]; 15 // Do any additional setup after loading the view. 16 17 // 添加所有子控制器 18 [self setUpAllChildViewController]; 19 20 } 21 22 // 如果通过模型设置控件的文字颜色,只能通过文本属性(富文本:颜色,字体,空心,阴影,图文混排) 23 // 在ios7之后,默认会把UITabBar上面的按钮图片渲染成蓝色 24 #pragma mark - 添加所有的子控制器 25 - (void)setUpAllChildViewController 26 { 27 // 首页 28 UIViewController *home = [[UIViewController alloc] init]; 29 // imageWithOriginalName:这个参数只能改变选中的按钮图片不为系统默认的蓝色,但是按钮的文字改不了,要在下面的封装部分修改。 30 [self setUpOneChildViewController:home image:[UIImage imageNamed:@"tabbar_home"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_home_selected"] title:@"首页"]; 31 home.view.backgroundColor = [UIColor greenColor]; 32 33 // 消息 34 UIViewController *message = [[UIViewController alloc] init]; 35 [self setUpOneChildViewController:message image:[UIImage imageNamed:@"tabbar_message_center"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_message_center_selected"] title:@"消息"]; 36 message.view.backgroundColor = [UIColor blueColor]; 37 38 // 发现 39 UIViewController *discover = [[UIViewController alloc] init]; 40 [self setUpOneChildViewController:discover image:[UIImage imageNamed:@"tabbar_discover"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_discover_selected"] title:@"发现"]; 41 discover.view.backgroundColor = [UIColor purpleColor]; 42 43 44 // 我 45 UIViewController *profile = [[UIViewController alloc] init]; 46 [self setUpOneChildViewController:profile image:[UIImage imageNamed:@"tabbar_profile"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_profile_selected"] title:@"我"]; 47 profile.view.backgroundColor = [UIColor lightGrayColor]; 48 } 49 50 #pragma mark - 添加一个子控制器 51 - (void)setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title 52 { 53 // 待传进标题 54 vc.tabBarItem.title = title; 55 // 待传进正常状态下的图片 56 vc.tabBarItem.image = image; 57 // 下面三行是让按钮的文字也变成窗口的颜色 58 NSMutableDictionary *att = [NSMutableDictionary dictionary]; // [NSForegroundColorAttributeName]传key,[UIColor orangeColor]传 value。 59 att[NSForegroundColorAttributeName] = [UIColor orangeColor]; 60 [vc.tarBarItem.setTitleTextAttributes:att forState: UIControlStateSelected]; 61 // 让所有子控制器都显示10条显示 62 vc.tabBarItem.badgeValue = @"10"; 63 // 传进选中时显示的图片 64 vc.tabBarItem.selectedImage = selectedImage; 65 66 [self addChildViewController:vc]; 67 } 68 69 @end
实例肆:接着看看修改过的” EmoTabBarController.m“,其实下面这段代码还可以再做封装。
57 // 下面三行是让按钮的文字也变成窗口的颜色 58 NSMutableDictionary *att = [NSMutableDictionary dictionary]; 59 [att setObject:[UIColor orangeColor] forKey:NSForegroundColorAttributeName]; 60 [vc.tarBarItem.setTitleTextAttributes:att forState: UIControlStateSelected];
用全局方法实现上面功能,如下代码:
1 // EmoTabBarController.m - 版本4 2 // Emo微博 3 4 #import "EmoTabBarController.h" 5 #import "UIImage+Image.h" 6 7 @interface EmoTabBarController () 8 @end 9 10 @implementation EmoTabBarController 11 12 // 什么时候调用:当第一次使用这个类或者子类的时候调用 13 // ”+ (void)initialize{}“作用:类的初始化,其实”+ (void)load{}“也是可以初始化类的 14 + (void)initialize 15 { 16 // 获取所有的tabBarItem外观标识,appearance只要一个类遵守UIAppearance,就能获取全局的外观,UIView 18 // 获取方法一: 19 // UITabBarItem *item = [UITabBarItem appearance]; 20 //为什么不用方法一的原因是:所有文件中,只要是协议里的 tabBarItem 都会被修改 21 22 // 方法二: 23 // self是指向EmoTabBarController 24 // 下面的代码意思是:仅仅获取当前这个类下面的所有tabBarItem,对其他文件中的 tabBarItem 无效。 25 UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil]; 26 27 NSMutableDictionary *att = [NSMutableDictionary dictionary]; 28 att[NSForegroundColorAttributeName] = [UIColor orangeColor]; 29 [item setTitleTextAttributes:att forState:UIControlStateSelected]; 30 } 31 32 - (void)viewDidLoad { 33 [super viewDidLoad]; 34 // Do any additional setup after loading the view. 35 36 // 添加所有子控制器 37 [self setUpAllChildViewController]; 38 } 39 40 // 如果通过模型设置控件的文字颜色,只能通过文本属性(富文本:颜色,字体,空心,阴影,图文混排) 41 // 在ios7之后,默认会把UITabBar上面的按钮图片渲染成蓝色 42 #pragma mark - 添加所有的子控制器 43 - (void)setUpAllChildViewController 44 { 45 // 首页 46 UIViewController *home = [[UIViewController alloc] init]; 47 [self setUpOneChildViewController:home image:[UIImage imageNamed:@"tabbar_home"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_home_selected"] title:@"首页"]; 48 home.view.backgroundColor = [UIColor greenColor]; 49 50 // 消息 51 UIViewController *message = [[UIViewController alloc] init]; 52 [self setUpOneChildViewController:message image:[UIImage imageNamed:@"tabbar_message_center"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_message_center_selected"] title:@"消息"]; 53 message.view.backgroundColor = [UIColor blueColor]; 54 55 // 发现 56 UIViewController *discover = [[UIViewController alloc] init]; 57 [self setUpOneChildViewController:discover image:[UIImage imageNamed:@"tabbar_discover"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_discover_selected"] title:@"发现"]; 58 discover.view.backgroundColor = [UIColor purpleColor]; 59 61 // 我 62 UIViewController *profile = [[UIViewController alloc] init]; 63 [self setUpOneChildViewController:profile image:[UIImage imageNamed:@"tabbar_profile"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_profile_selected"] title:@"我"]; 64 profile.view.backgroundColor = [UIColor lightGrayColor]; 65 } 66 67 #pragma mark - 添加一个子控制器 68 - (void)setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title 69 { 70 vc.tabBarItem.title = title; 71 vc.tabBarItem.image = image; 72 //***此处被剪切到上面作为全局效果***// 73 vc.tabBarItem.badgeValue = @"10"; 74 vc.tabBarItem.selectedImage = selectedImage; 75 [self addChildViewController:vc]; 76 } 77 79 @end
经过上述肆个案例之后,展示效果为:
实例伍:想要修改下面横栏按钮的位置,需要自定义一个 tabBar。新建一个UITabBar:
1 // EmoTabBar.h - 版本1 2 // Emo微博 3 4 #import <UIKit/UIKit.h> 5 6 @interface EmoTabBar : UITabBar 7 @end
1 // EmoTabBar.m - 版本1 2 // Emo微博 3 4 #import "EmoTabBar.h" 5 @interface EmoTabBar () 6 @property (nonatomic, weak) UIButton *plusButton; 7 @end 8 9 @implementation EmoTabBar 10 11 - (UIButton *)plusButton 12 { 13 if (_plusButton == nil) { 14 15 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 16 [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal]; 17 [btn setImage:[UIImage imageNamed:@"tabbar_compose_background_icon_add"] forState:UIControlStateHighlighted]; 18 [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal]; 19 [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted]; 20 21 // 默认按钮的尺寸跟背景图片一样大 22 // sizeToFit:默认会根据按钮的背景图片或者image和文字计算出按钮的最合适的尺寸 23 [btn sizeToFit]; 24 25 _plusButton = btn; 26 27 [self addSubview:_plusButton]; 28 29 } 30 return _plusButton; 31 } 32 33 // self.items UITabBarItem模型,有多少个子控制器就有多少个UITabBarItem模型 34 // 调整子控件的位置 35 - (void)layoutSubviews 36 { 37 [super layoutSubviews]; 38 39 CGFloat w = self.bounds.size.width; 40 CGFloat h = self.bounds.size.height; 41 42 CGFloat btnX = 0; 43 CGFloat btnY = 0; 44 CGFloat btnW = w / (self.items.count + 1); 45 CGFloat btnH = self.bounds.size.height; 46 47 int i = 0; 48 // 调整系统自带的tabBar上的按钮位置 49 for (UIView *tabBarButton in self.subviews) { 50 // 判断下是否是UITabBarButton 51 if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton" )]) { 52 if (i == 2) { 53 i = 3; 54 } 55 btnX = i * btnW; 56 57 tabBarButton.frame = CGRectMake(btnX, btnY, btnW, btnH); 58 59 i++; 60 } 61 } 62 63 64 // 设置添加按钮的位置 65 self.plusButton.center = CGPointMake(w * 0.5, h * 0.5); 66 } 67 68 @end
1 // EmoTabBarController.m - 版本5 2 // Emo微博 3 4 5 #import "EmoTabBarController.h" 6 #import "UIImage+Image.h" 7 #import "EmoTabBar.h" 8 9 #import <objc/message.h> 10 11 @interface EmoTabBarController () 12 @end 13 14 @implementation EmoTabBarController 15 16 // 什么调用:当第一次使用这个类或者子类的时候调用 17 // 作用:初始化类 18 // appearance只要一个类遵守UIAppearance,就能获取全局的外观,UIView 19 + (void)initialize 20 { 21 // 获取所有的tabBarItem外观标识 22 // UITabBarItem *item = [UITabBarItem appearance]; 23 24 // self -> CZTabBarController 25 // 获取当前这个类下面的所有tabBarItem 26 UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil]; 27 28 NSMutableDictionary *att = [NSMutableDictionary dictionary]; 29 att[NSForegroundColorAttributeName] = [UIColor orangeColor]; 30 // [att setObject:[UIColor orangeColor] forKey:NSForegroundColorAttributeName]; 31 32 [item setTitleTextAttributes:att forState:UIControlStateSelected]; 33 } 34 35 - (void)viewDidLoad { 36 [super viewDidLoad]; 37 // Do any additional setup after loading the view. 38 // 添加所有子控制器 39 [self setUpAllChildViewController]; 40 // 自定义tabBar 41 EmoTabBar *tabBar = [[EmoTabBar alloc] initWithFrame:self.tabBar.frame]; 42 NSLog(@"%@",self.tabBar); 43 // 利用KVC把readly的属性改 44 [self setValue:tabBar forKeyPath:@"tabBar"]; 45 // objc_msgSend(self, @selector(setTabBar:),tabBar);//xcode 不希望用户使用消息机制 46 NSLog(@"%@",self.tabBar); 47 // self.tabBar = tabBar; 48 // 修改系统tabBar上面的按钮的位置 49 // NSLog(@"%@",self.tabBar.subviews); 50 // 51 // self.tabBar = 52 } 53 54 - (void)viewWillAppear:(BOOL)animated 55 { 56 [super viewWillAppear:animated]; 57 58 } 59 60 // 如果通过模型设置控件的文字颜色,只能通过文本属性(富文本:颜色,字体,空心,阴影,图文混排) 61 62 // 在ios7之后,默认会把UITabBar上面的按钮图片渲染成蓝色 63 #pragma mark - 添加所有的子控制器 64 - (void)setUpAllChildViewController 65 { 66 // 首页 67 UIViewController *home = [[UIViewController alloc] init]; 68 [self setUpOneChildViewController:home image:[UIImage imageNamed:@"tabbar_home"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_home_selected"] title:@"首页"]; 69 home.view.backgroundColor = [UIColor greenColor]; 70 71 // 消息 72 UIViewController *message = [[UIViewController alloc] init]; 73 [self setUpOneChildViewController:message image:[UIImage imageNamed:@"tabbar_message_center"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_message_center_selected"] title:@"消息"]; 74 message.view.backgroundColor = [UIColor blueColor]; 75 76 // 发现 77 UIViewController *discover = [[UIViewController alloc] init]; 78 [self setUpOneChildViewController:discover image:[UIImage imageNamed:@"tabbar_discover"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_discover_selected"] title:@"发现"]; 79 discover.view.backgroundColor = [UIColor purpleColor]; 80 81 82 // 我 83 UIViewController *profile = [[UIViewController alloc] init]; 84 [self setUpOneChildViewController:profile image:[UIImage imageNamed:@"tabbar_profile"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_profile_selected"] title:@"我"]; 85 profile.view.backgroundColor = [UIColor lightGrayColor]; 86 } 87 88 #pragma mark - 添加一个子控制器 89 - (void)setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title 90 { 91 vc.tabBarItem.title = title; 92 vc.tabBarItem.image = image; 93 vc.tabBarItem.badgeValue = @"10"; 94 vc.tabBarItem.selectedImage = selectedImage; 95 96 [self addChildViewController:vc]; 97 } 98 99 100 @end
时间: 2024-10-25 00:03:08