swift中怎么更改uitabbarswift 创建controllerr标签变成透明

&&国之画&&&&&&
版权所有 京ICP备号-2
迷上了代码!&&国之画&&&&&&
&& &&&&&&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!iOS开发项目篇自定义UITabBar - 51iOS.net
iOS开发项目篇自定义UITabBar
&>>&&>>&iOS开发项目篇自定义UITabBar
0 次,平均分
iOS开发项目篇&27自定义UITabBar
一、自定义
(1)新建一个继承自UITabBar的类,自定义一个UITabBar
(2)用自定义的UITabBar换掉的UItabBar(使用了KVC)
(3)监听控制器的切换,只要控制器一切换,就调用代理方法强制重新布局子控件(内部会调用layoutSubviews)。
YYTabBar.m文件代码:
& 2 // &YYTabBar.m
& 5 #import &YYTabBar.h&
& 7 @interface YYTabBar()
& 8 @property (nonatomic, weak) UIButton *plusB
&11 @implementation YYTabBar
&13 - (id)initWithFrame:(CGRect)frame
&15 & & self = [super initWithFrame:frame];
&16 & & if (self) {
&17 & & & & // 添加加号按钮
&18 & & & & [self setupPlusButton];
&24 &* &添加加号按钮
&26 - (void)setupPlusButton
&28 & & UIButton *plusButton = [[UIButton alloc] init];
&29 & & // 设置背景
&30 & & [plusButton setBackgroundImage:[UIImage imageWithName:@&tabbar_compose_button&] forState:UIControlStateNormal];
&31 & & [plusButton setBackgroundImage:[UIImage imageWithName:@&tabbar_compose_button_highlighted&] forState:UIControlStateHighlighted];
&32 & & // 设置图标
&33 & & [plusButton setImage:[UIImage imageWithName:@&tabbar_compose_icon_add&] forState:UIControlStateNormal];
&34 & & [plusButton setImage:[UIImage imageWithName:@&tabbar_compose_icon_add_highlighted&] forState:UIControlStateHighlighted];
&35 & & [plusButton addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
&36 & & // 添加
&37 & & [self addSubview:plusButton];
&38 & & self.plusButton = plusB
&41 - (void)plusClick
&43 & & YYLog(@&plusClick----&);
&47 &* &布局子控件
&49 - (void)layoutSubviews
&51 & & [super layoutSubviews];
&53 & & // 设置plusButton的frame
&54 & & [self setupPlusButtonFrame];
&56 & & // 设置所有tabbarButton的frame
&57 & & [self setupAllTabBarButtonsFrame];
&61 &* &设置所有plusButton的frame
&63 - (void)setupPlusButtonFrame
&65 & & self.plusButton.size = self.plusButton.currentBackgroundImage.
&66 & & self.plusButton.center = CGPointMake(self.width * 0.5, self.height * 0.5);
&70 &* &设置所有tabbarButton的frame
&72 - (void)setupAllTabBarButtonsFrame
&74 & & int index = 0;
&76 & & // 遍历所有的button
&77 & & for (UIView *tabBarButton in self.subviews) {
&78 & & & & // 如果不是UITabBarButton, 直接跳过
&79 & & & & if (![tabBarButton isKindOfClass:NSClassFromString(@&UITabBarButton&)])
&80 & & & &&
&81 & & & & // 根据索引调整位置
&82 & & & & [self setupTabBarButtonFrame:tabBarButton atIndex:index];
&83 & & & &&
&84 & & & & // 遍历UITabBarButton中的所有子控件
&85 & & & & [self setupTabBarButtonTextColor:tabBarButton atIndex:index];
&86 & & & &&
&87 & & & & // 索引增加
&88 & & & & index++;
&93 &* &设置某个按钮的文字颜色
&95 &* &@param tabBarButton 需要设置的按钮
&96 &* &@param index & & & &按钮所在的索引
&98 - (void)setupTabBarButtonTextColor:(UIView *)tabBarButton atIndex:(int)index
100 & & // 选中按钮的索引
101 & & int selectedIndex = [self.items indexOfObject:self.selectedItem];
103 & & for (UILabel *label in tabBarButton.subviews) {
104 & & & & // 说明不是个Label
105 & & & & if (![label isKindOfClass:[UILabel class]])
107 & & & & // 设置字体
108 & & & & label.font = [UIFont systemFontOfSize:10];
109 & & & & if (selectedIndex == index) { // 说明这个Button选中, 设置label颜色为橙色
110 & & & & & & label.textColor = [UIColor orangeColor];
111 & & & & } else { // 说明这个Button没有选中, 设置label颜色为黑色
112 & & & & & & label.textColor = [UIColor blackColor];
113 & & & & }
118 &* &设置某个按钮的frame
120 &* &@param tabBarButton 需要设置的按钮
121 &* &@param index & & & &按钮所在的索引
123 - (void)setupTabBarButtonFrame:(UIView *)tabBarButton atIndex:(int)index
125 & & // 计算button的尺寸
126 & & CGFloat buttonW = self.width / (self.items.count + 1);
127 & & CGFloat buttonH = self.
129 & & tabBarButton.width = buttonW;
130 & & tabBarButton.height = buttonH;
131 & & if (index &= 2) {
132 & & & & tabBarButton.x = buttonW * (index + 1);
133 & & } else {
134 & & & & tabBarButton.x = buttonW *
136 & & tabBarButton.y = 0;
在YYTabBarViewController.m文件中使用自定义的UITabBar
&2 // &YYTabBarViewController.m
&5 #import &YYTabBarViewController.h&
&6 #import &YYHomeTableViewController.h&
&7 #import &YYDiscoverViewController.h&
&8 #import &YYMessageViewController.h&
&9 #import &YYProfileViewController.h&
10 #import &UIImage+Extension.h&
11 #import &YYNavigationViewController.h&
12 #import &YYTabBar.h&
14 @interface YYTabBarViewController ()&UITabBarControllerDelegate&
18 @implementation YYTabBarViewController
21 - (void)viewDidLoad
23 & & [super viewDidLoad];
24 & & //添加四个子控制器
25 & & YYHomeTableViewController *home=[[YYHomeTableViewController alloc]init];
26 & & [self addOneChildVc:home title:@&首页& imageName:@&tabbar_home& selectedImageName:@&tabbar_home_selected&];
29 & & YYMessageViewController *message=[[YYMessageViewController alloc]init];
30 & & [self addOneChildVc:message title:@&消息& imageName:@&tabbar_message_center& selectedImageName:@&tabbar_message_center_selected&];
32 & & YYDiscoverViewController *discover=[[YYDiscoverViewController alloc]init];
33 & & [self addOneChildVc:discover title:@&发现& imageName:@&tabbar_discover& selectedImageName:@&tabbar_discover_selected&];
35 & & YYProfileViewController *profile=[[YYProfileViewController alloc]init];
36 & & [self addOneChildVc:profile title:@&我& imageName:@&tabbar_profile& selectedImageName:@&tabbar_profile_selected&];
39 & & // 调整tabbar
40 & & YYTabBar *customTabBar = [[YYTabBar alloc] init];
41 & & customTabBar.backgroundImage = [UIImage imageWithName:@&tabbar_background&];
42 & & customTabBar.selectionIndicatorImage = [UIImage imageWithName:@&navigationbar_button_background&];
43 & & // 更换系统自带的tabbar
44 & & [self setValue:customTabBar forKeyPath:@&tabBar&];
46 & & // 设置代理(监听控制器的切换, 控制器一旦切换了子控制器,就会调用代理的tabBarController:didSelectViewController:)
47 & & self.delegate =
51 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
53 & & // 强制重新布局子控件(内部会调用layouSubviews)
54 & & [self.tabBar setNeedsLayout];
58 &* &添加一个子控制器
60 &* &@param childVC & & & & & 子控制对象
61 &* &@param title & & & & & & 标题
62 &* &@param imageName & & & & 图标
63 &* &@param selectedImageName 选中时的图标
65 -(void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
67 & & //随机设置子控制器的背景颜色
68 // & &childVc.view.backgroundColor=YYRandomC
70 & & //设置标题
71 & & childVc.title= &//相当于设置了后两者的标题
72 // & &childVc.navigationItem.title=//设置导航栏的标题
73 // & &childVc.tabBarItem.title=//设置tabbar上面的标题
75 & & //设置图标
76 & & childVc.tabBarItem.image=[UIImage imageWithName:imageName];
77 & & //设置选中时的图标
78 & & UIImage *selectedImage=[UIImage imageWithName:selectedImageName];
81 & & if (iOS7) {
82 & & & & // 声明这张图片用原图(别渲染)
83 & & & & selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
85 & & childVc.tabBarItem.selectedImage = selectedI
87 & & &// 添加为tabbar控制器的子控制器
88 & & YYNavigationViewController *nav=[[YYNavigationViewController alloc]initWithRootViewController:childVc];
90 & & [self addChildViewController:nav];
95 // 在iOS7中, 会对selectedImage的图片进行再次渲染为蓝色
96 // 要想显示原图, 就必须得告诉它: 不要渲染
98 // Xcode的插件安装路径: /Users/用户名/Library/Application Support/Developer/Shared/Xcode/Plug-ins
实现效果(iOS7中)
关键代码:更换系统自带的tabbar & & &[self setValue:customTabBar forKeyPath:@&tabBar&];
说明:不能直接使用self.tabBar的方式调用set方法进行更换,因为这个属性是只读的,这里的实现是直接使用KVC把它内部的下划线常量给修改了。
问题说明:
(1)上面的代码下方的UITabBar是透明的。因为ios6中的背景图片是白色的,而ios7中的背景图片是透明的。有两种解决方式,一种是在mainbundle中直接把ios7对应的那张图片素材删除即可,但是这种方法会导致ios7中的穿透效果没有。第二种解决方式,判断当前系统版本,如果当前是ios7则不需要设置。
(2)子控件的frame没必要每次都设置,只需要设置一次就可以了。切换控制器的目的只有一个,就是改变默认和选中状态下文字的颜色。
完善后的代码:
& YYTabBar.m文件
& 2 // &YYTabBar.m
& 5 #import &YYTabBar.h&
& 7 @interface YYTabBar()
& 8 @property (nonatomic, weak) UIButton *plusB
&11 @implementation YYTabBar
&13 - (id)initWithFrame:(CGRect)frame
&15 & & self = [super initWithFrame:frame];
&16 & & if (self) {
&17 & & & & if (!iOS7) {
&18 & & & & & & self.backgroundImage = [UIImage imageWithName:@&tabbar_background&];
&19 & & & & }
&20 & & & & self.selectionIndicatorImage = [UIImage imageWithName:@&navigationbar_button_background&];
&21 & & & & // 添加加号按钮
&22 & & & & [self setupPlusButton];
&28 &* &添加加号按钮
&30 - (void)setupPlusButton
&32 & & UIButton *plusButton = [[UIButton alloc] init];
&33 & & // 设置背景
&34 & & [plusButton setBackgroundImage:[UIImage imageWithName:@&tabbar_compose_button&] forState:UIControlStateNormal];
&35 & & [plusButton setBackgroundImage:[UIImage imageWithName:@&tabbar_compose_button_highlighted&] forState:UIControlStateHighlighted];
&36 & & // 设置图标
&37 & & [plusButton setImage:[UIImage imageWithName:@&tabbar_compose_icon_add&] forState:UIControlStateNormal];
&38 & & [plusButton setImage:[UIImage imageWithName:@&tabbar_compose_icon_add_highlighted&] forState:UIControlStateHighlighted];
&39 & & [plusButton addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
&40 & & // 添加
&41 & & [self addSubview:plusButton];
&42 & & self.plusButton = plusB
&45 - (void)plusClick
&47 & & YYLog(@&plusClick----&);
&51 &* &布局子控件
&53 - (void)layoutSubviews
&55 & & [super layoutSubviews];
&57 & & // 设置plusButton的frame
&58 & & [self setupPlusButtonFrame];
&60 & & // 设置所有tabbarButton的frame
&61 & & [self setupAllTabBarButtonsFrame];
&65 &* &设置所有plusButton的frame
&67 - (void)setupPlusButtonFrame
&69 & & self.plusButton.size = self.plusButton.currentBackgroundImage.
&70 & & self.plusButton.center = CGPointMake(self.width * 0.5, self.height * 0.5);
&74 &* &设置所有tabbarButton的frame
&76 - (void)setupAllTabBarButtonsFrame
&78 & & int index = 0;
&80 & & // 遍历所有的button
&81 & & for (UIView *tabBarButton in self.subviews) {
&82 & & & & // 如果不是UITabBarButton, 直接跳过
&83 & & & & if (![tabBarButton isKindOfClass:NSClassFromString(@&UITabBarButton&)])
&84 & & & &&
&85 & & & & // 根据索引调整位置
&86 & & & & [self setupTabBarButtonFrame:tabBarButton atIndex:index];
&87 // & & & &
&88 // & & & &// 遍历UITabBarButton中的所有子控件
&89 // & & & &[self setupTabBarButtonTextColor:tabBarButton atIndex:index];
&90 & & & &&
&91 & & & & // 索引增加
&92 & & & & index++;
&97 // * &设置某个按钮的文字颜色
&99 // * &@param tabBarButton 需要设置的按钮
100 // * &@param index & & & &按钮所在的索引
102 //- (void)setupTabBarButtonTextColor:(UIView *)tabBarButton atIndex:(int)index
104 // & &// 选中按钮的索引
105 // & &int selectedIndex = [self.items indexOfObject:self.selectedItem];
106 // & &
107 // & &for (UILabel *label in tabBarButton.subviews) {
108 // & & & &// 说明不是个Label
109 // & & & &if (![label isKindOfClass:[UILabel class]])
111 // & & & &// 设置字体
112 // & & & &label.font = [UIFont systemFontOfSize:10];
113 // & & & &if (selectedIndex == index) { // 说明这个Button选中, 设置label颜色为橙色
114 // & & & & & &label.textColor = [UIColor orangeColor];
115 // & & & &} else { // 说明这个Button没有选中, 设置label颜色为黑色
116 // & & & & & &label.textColor = [UIColor blackColor];
117 // & & & &}
118 // & &}
122 &* &设置某个按钮的frame
124 &* &@param tabBarButton 需要设置的按钮
125 &* &@param index & & & &按钮所在的索引
127 - (void)setupTabBarButtonFrame:(UIView *)tabBarButton atIndex:(int)index
129 & & // 计算button的尺寸
130 & & CGFloat buttonW = self.width / (self.items.count + 1);
131 & & CGFloat buttonH = self.
133 & & tabBarButton.width = buttonW;
134 & & tabBarButton.height = buttonH;
135 & & if (index &= 2) {
136 & & & & tabBarButton.x = buttonW * (index + 1);
137 & & } else {
138 & & & & tabBarButton.x = buttonW *
140 & & tabBarButton.y = 0;
& YYTabBarViewController.m文件
& 2 // &YYTabBarViewController.m
& 5 #import &YYTabBarViewController.h&
& 6 #import &YYHomeTableViewController.h&
& 7 #import &YYDiscoverViewController.h&
& 8 #import &YYMessageViewController.h&
& 9 #import &YYProfileViewController.h&
&10 #import &UIImage+Extension.h&
&11 #import &YYNavigationViewController.h&
&12 #import &YYTabBar.h&
&14 @interface YYTabBarViewController ()&UITabBarControllerDelegate&
&18 @implementation YYTabBarViewController
&21 - (void)viewDidLoad
&23 & & [super viewDidLoad];
&24 & & //添加四个子控制器
&25 & & YYHomeTableViewController *home=[[YYHomeTableViewController alloc]init];
&26 & & [self addOneChildVc:home title:@&首页& imageName:@&tabbar_home& selectedImageName:@&tabbar_home_selected&];
&29 & & YYMessageViewController *message=[[YYMessageViewController alloc]init];
&30 & & [self addOneChildVc:message title:@&消息& imageName:@&tabbar_message_center& selectedImageName:@&tabbar_message_center_selected&];
&32 & & YYDiscoverViewController *discover=[[YYDiscoverViewController alloc]init];
&33 & & [self addOneChildVc:discover title:@&发现& imageName:@&tabbar_discover& selectedImageName:@&tabbar_discover_selected&];
&35 & & YYProfileViewController *profile=[[YYProfileViewController alloc]init];
&36 & & [self addOneChildVc:profile title:@&我& imageName:@&tabbar_profile& selectedImageName:@&tabbar_profile_selected&];
&39 & & // 调整tabbar
&40 & & YYTabBar *customTabBar = [[YYTabBar alloc] init];
&41 & & // 更换系统自带的tabbar
&42 & & [self setValue:customTabBar forKeyPath:@&tabBar&];
&44 // & &// 设置代理(监听控制器的切换, 控制器一旦切换了子控制器,就会调用代理的tabBarController:didSelectViewController:)
&45 // & &self.delegate =
&49 //- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
&51 // & &// 强制重新布局子控件(内部会调用layouSubviews)
&52 // & &[self.tabBar setNeedsLayout];
&57 &* &添加一个子控制器
&59 &* &@param childVC & & & & & 子控制对象
&60 &* &@param title & & & & & & 标题
&61 &* &@param imageName & & & & 图标
&62 &* &@param selectedImageName 选中时的图标
&64 -(void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
&66 & & //随机设置子控制器的背景颜色
&67 // & &childVc.view.backgroundColor=YYRandomC
&69 & & //设置标题
&70 & & childVc.title= &//相当于设置了后两者的标题
&71 // & &childVc.navigationItem.title=//设置导航栏的标题
&72 // & &childVc.tabBarItem.title=//设置tabbar上面的标题
&74 & & //设置图标
&75 & & childVc.tabBarItem.image=[UIImage imageWithName:imageName];
&76 & & //设置选中时的图标
&77 & & UIImage *selectedImage=[UIImage imageWithName:selectedImageName];
&79 & & //设置tabBarItem普通状态下文字的颜色
&80 & & NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary];
&81 & & textAttrs[UITextAttributeTextColor]=[UIColor blackColor];
&82 & & textAttrs[UITextAttributeFont]=[UIFont systemFontOfSize:10];
&83 & & [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
&85 & & //设置tabBarItem普通状态下文字的颜色
&86 & & NSMutableDictionary *selectedtextAttrs=[NSMutableDictionary dictionary];
&87 & & selectedtextAttrs[UITextAttributeTextColor]=[UIColor orangeColor];
&88 & & [childVc.tabBarItem setTitleTextAttributes:selectedtextAttrs forState:UIControlStateSelected];
&90 & & if (iOS7) {
&91 & & & & // 声明这张图片用原图(别渲染)
&92 & & & & selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
&94 & & childVc.tabBarItem.selectedImage = selectedI
&96 & & &// 添加为tabbar控制器的子控制器
&97 & & YYNavigationViewController *nav=[[YYNavigationViewController alloc]initWithRootViewController:childVc];
&99 & & [self addChildViewController:nav];
104 // 在iOS7中, 会对selectedImage的图片进行再次渲染为蓝色
105 // 要想显示原图, 就必须得告诉它: 不要渲染
107 // Xcode的插件安装路径: /Users/用户名/Library/Application Support/Developer/Shared/Xcode/Plug-ins
三、加号按钮的点击事件
使用代理方法,当点击加号按钮的时候,通知UITabBarController以模态的方式弹出控制器。
实现代码:
1.设置代理
YYTabBar.h文件
&2 // &YYTabBar.h
&5 #import &UIKit/UIKit.h&
&7 @class YYTabB
&8 @protocol YYTabBarDelegate &NSObject&
&9 -(void)tabBarDidClickedPlusButton:(YYTabBar *)tabB
12 @interface YYTabBar : UITabBar
13 @property(nonatomic,weak)id&YYTabBarDelegate&
YYTabBar.m文件
& 2 // &YYTabBar.m
& 5 #import &YYTabBar.h&
& 7 @interface YYTabBar()
& 8 @property (nonatomic, weak) UIButton *plusB
&11 @implementation YYTabBar
&13 - (id)initWithFrame:(CGRect)frame
&15 & & self = [super initWithFrame:frame];
&16 & & if (self) {
&17 & & & & if (!iOS7) {
&18 & & & & & & self.backgroundImage = [UIImage imageWithName:@&tabbar_background&];
&19 & & & & }
&20 & & & & self.selectionIndicatorImage = [UIImage imageWithName:@&navigationbar_button_background&];
&21 & & & & // 添加加号按钮
&22 & & & & [self setupPlusButton];
&29 &* &添加加号按钮
&31 - (void)setupPlusButton
&33 & & UIButton *plusButton = [[UIButton alloc] init];
&34 & & // 设置背景
&35 & & [plusButton setBackgroundImage:[UIImage imageWithName:@&tabbar_compose_button&] forState:UIControlStateNormal];
&36 & & [plusButton setBackgroundImage:[UIImage imageWithName:@&tabbar_compose_button_highlighted&] forState:UIControlStateHighlighted];
&37 & & // 设置图标
&38 & & [plusButton setImage:[UIImage imageWithName:@&tabbar_compose_icon_add&] forState:UIControlStateNormal];
&39 & & [plusButton setImage:[UIImage imageWithName:@&tabbar_compose_icon_add_highlighted&] forState:UIControlStateHighlighted];
&40 & & [plusButton addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
&41 & & // 添加
&42 & & [self addSubview:plusButton];
&43 & & self.plusButton = plusB
&46 - (void)plusClick
&48 & & //设置代理
&49 & & if ([self.delegate respondsToSelector:@selector(tabBarDidClickedPlusButton:)]) {
&50 & & & & [self.delegate tabBarDidClickedPlusButton:self];
&55 &* &布局子控件
&57 - (void)layoutSubviews
&59 & & [super layoutSubviews];
&61 & & // 设置plusButton的frame
&62 & & [self setupPlusButtonFrame];
&64 & & // 设置所有tabbarButton的frame
&65 & & [self setupAllTabBarButtonsFrame];
&69 &* &设置所有plusButton的frame
&71 - (void)setupPlusButtonFrame
&73 & & self.plusButton.size = self.plusButton.currentBackgroundImage.
&74 & & self.plusButton.center = CGPointMake(self.width * 0.5, self.height * 0.5);
&78 &* &设置所有tabbarButton的frame
&80 - (void)setupAllTabBarButtonsFrame
&82 & & int index = 0;
&84 & & // 遍历所有的button
&85 & & for (UIView *tabBarButton in self.subviews) {
&86 & & & & // 如果不是UITabBarButton, 直接跳过
&87 & & & & if (![tabBarButton isKindOfClass:NSClassFromString(@&UITabBarButton&)])
&88 & & & &&
&89 & & & & // 根据索引调整位置
&90 & & & & [self setupTabBarButtonFrame:tabBarButton atIndex:index];
&91 & & & &&
&92 & & & & // 索引增加
&93 & & & & index++;
&99 &* &设置某个按钮的frame
101 &* &@param tabBarButton 需要设置的按钮
102 &* &@param index & & & &按钮所在的索引
104 - (void)setupTabBarButtonFrame:(UIView *)tabBarButton atIndex:(int)index
106 & & // 计算button的尺寸
107 & & CGFloat buttonW = self.width / (self.items.count + 1);
108 & & CGFloat buttonH = self.
110 & & tabBarButton.width = buttonW;
111 & & tabBarButton.height = buttonH;
112 & & if (index &= 2) {
113 & & & & tabBarButton.x = buttonW * (index + 1);
114 & & } else {
115 & & & & tabBarButton.x = buttonW *
117 & & tabBarButton.y = 0;
2.新建一个发送消息的控制器,其继承自UIViewController
YYComposeViewController.m文件
&2 // &YYComposeViewController.m
&5 #import &YYComposeViewController.h&
&7 @interface YYComposeViewController ()
11 @implementation YYComposeViewController
14 - (void)viewDidLoad
16 & & [super viewDidLoad];
17 & & self.title=@&发消息&;
18 & & self.view.backgroundColor=[UIColor yellowColor];
19 & & self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@&取消& style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)];
20 & & self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@&发送& style:UIBarButtonItemStyleBordered target:self action:@selector(send)];
21 & & self.navigationItem.rightBarButtonItem.enabled=NO;
24 -(void)send
26 & & YYLog(@&发送----&);
29 -(void)cancel
31 & & [self dismissViewControllerAnimated:YES completion:nil];
3.实现代理方法,监听加号按钮的点击事件
YYTabBarViewController.m文件
& 2 // &YYTabBarViewController.m
& 5 #import &YYTabBarViewController.h&
& 6 #import &YYHomeTableViewController.h&
& 7 #import &YYDiscoverViewController.h&
& 8 #import &YYMessageViewController.h&
& 9 #import &YYProfileViewController.h&
&10 #import &UIImage+Extension.h&
&11 #import &YYNavigationViewController.h&
&12 #import &YYTabBar.h&
&13 #import &YYComposeViewController.h&
&15 @interface YYTabBarViewController ()&UITabBarControllerDelegate,YYTabBarDelegate&
&19 @implementation YYTabBarViewController
&22 - (void)viewDidLoad
&24 & & [super viewDidLoad];
&25 & & //添加四个子控制器
&26 & & [self addAllChildVcs];
&28 & & // 调整tabbar
&29 & & [self addCustomTabBar];
&32 -(void)addAllChildVcs
&34 & & YYHomeTableViewController *home=[[YYHomeTableViewController alloc]init];
&35 & & [self addOneChildVc:home title:@&首页& imageName:@&tabbar_home& selectedImageName:@&tabbar_home_selected&];
&38 & & YYMessageViewController *message=[[YYMessageViewController alloc]init];
&39 & & [self addOneChildVc:message title:@&消息& imageName:@&tabbar_message_center& selectedImageName:@&tabbar_message_center_selected&];
&41 & & YYDiscoverViewController *discover=[[YYDiscoverViewController alloc]init];
&42 & & [self addOneChildVc:discover title:@&发现& imageName:@&tabbar_discover& selectedImageName:@&tabbar_discover_selected&];
&44 & & YYProfileViewController *profile=[[YYProfileViewController alloc]init];
&45 & & [self addOneChildVc:profile title:@&我& imageName:@&tabbar_profile& selectedImageName:@&tabbar_profile_selected&];
&48 -(void)addCustomTabBar
&50 & & YYTabBar *customTabBar = [[YYTabBar alloc] init];
&51 & & //设置代理
&52 & & customTabBar.delegate=
&53 & & // 更换系统自带的tabbar
&54 & & [self setValue:customTabBar forKeyPath:@&tabBar&];
&58 #pragma mark-YYTabBarDelegate
&59 -(void)tabBarDidClickedPlusButton:(YYTabBar *)tabBar
&61 & & //弹出发微博的控制器
&62 & & YYComposeViewController *compose=[[YYComposeViewController alloc]init];
&63 & & YYNavigationViewController *nav=[[YYNavigationViewController alloc]initWithRootViewController:compose];
&64 & & [self presentViewController:nav animated:YES completion:nil];
&69 &* &添加一个子控制器
&71 &* &@param childVC & & & & & 子控制对象
&72 &* &@param title & & & & & & 标题
&73 &* &@param imageName & & & & 图标
&74 &* &@param selectedImageName 选中时的图标
&76 -(void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
&78 & & //随机设置子控制器的背景颜色
&79 // & &childVc.view.backgroundColor=YYRandomC
&81 & & //设置标题
&82 & & childVc.title= &//相当于设置了后两者的标题
&85 & & //设置图标
&86 & & childVc.tabBarItem.image=[UIImage imageWithName:imageName];
&87 & & //设置选中时的图标
&88 & & UIImage *selectedImage=[UIImage imageWithName:selectedImageName];
&90 & & //设置tabBarItem普通状态下文字的颜色
&91 & & NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary];
&92 & & textAttrs[UITextAttributeTextColor]=[UIColor blackColor];
&93 & & textAttrs[UITextAttributeFont]=[UIFont systemFontOfSize:10];
&94 & & [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
&96 & & //设置tabBarItem普通状态下文字的颜色
&97 & & NSMutableDictionary *selectedtextAttrs=[NSMutableDictionary dictionary];
&98 & & selectedtextAttrs[UITextAttributeTextColor]=[UIColor orangeColor];
&99 & & [childVc.tabBarItem setTitleTextAttributes:selectedtextAttrs forState:UIControlStateSelected];
101 & & if (iOS7) {
102 & & & & // 声明这张图片用原图(别渲染)
103 & & & & selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
105 & & childVc.tabBarItem.selectedImage = selectedI
107 & & &// 添加为tabbar控制器的子控制器
108 & & YYNavigationViewController *nav=[[YYNavigationViewController alloc]initWithRootViewController:childVc];
110 & & [self addChildViewController:nav];
来源:,欢迎分享本文,转载请注明出处来自
51ios.net也许会成为你的良师益友,或是你的技术加油站,这里有大量活跃读者和作者,我们关注iOS、swift、iOS开发、iOS教程、iOS开发教程、iOS代码、swift代码、swift 教程、oc、Objective-c、iOS视频、swift视频,也会提供程序人生的文章,51iOS是一个值得收藏的网站,是移动开发者必备网站。希望你每天在51iOS.net每天进步一点点。
如果您有站务合作方面的需求,请通过以下方式联系我。QQ: xxxxxxxEmail: l_ch_(#换成@)
您也可以使用第三方帐号快捷登录
扫一扫二维码分享

我要回帖

更多关于 swift controller跳转 的文章

 

随机推荐