两种方法实现类微博tabBar,并跳转页面隐藏
2016-03-08 00:08
489 查看
方法一
用自定义tabBar替换系统tabBar, 然后调用self.hidesBottomBarWhenPushed = YES直接隐藏
代码实现
自定义tabBar
首先自定义一个tabBar继承于UItabBar在自定义tabBar的.h文件中输入
@property (nonatomic, weak) UIButton *pButton;
在.m文件开始布局自定义tabBar
- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { //添加发布按钮 UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom]; [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal]; [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected]; [self addSubview:publishButton]; self.pButton = publishButton; } return self; } - (void)layoutSubviews { [super layoutSubviews]; //设置发布按钮的 frame self.pButton.size = self.publishButton.currentBackgroundImage.size; self.pButton.center = CGPointMake(self.width * 0.5, self.height * 0.5); //设置其他 UITabBarButton 的 frame CGFloat buttonY = 0; CGFloat buttonW = self.width / 5; CGFloat buttonH = self.height; NSInteger buttonIndex = 0; for (UIView *button in self.subviews) { /** 计算按钮 X 值, 为了空出中间的距离 从第二个tabBarButton起, 第三个按钮为我们自定义的按钮, 第四个第五个为系统的按钮 index从0起, 那么当buttonIndex > 1 也就是我们看到的第四个按钮起 X值就为按钮宽度 * (按钮下标 + 1) */ CGFloat buttonX = 0; if (buttonIndex > 1) { buttonX = buttonW * (buttonIndex + 1); } else { buttonX = buttonW * buttonIndex; } //如果按钮不是tabBarButton, 那么不管它, 继续 if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) { continue; } button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH); //增加索引 buttonIndex++; } }
自定义tabBarController
在tabBarController中用自定义tabBar替换系统自身tabBar注意:
self.tabBar = sinaTabBar;
是行不通的, 大家可以试试, 因为self.tabBar是私有属性.
但是可以用KVC替换
SinaTabBar *sinaTabBar = [[SinaTabBar alloc] init]; [sinaTabBar.publishButton addTarget:self action:@selector(popToView) forControlEvents:UIControlEventTouchUpInside]; //替换系统tabBar为自定义的 [self setValue:sinaTabBar forKeyPath:@"tabBar"];
完整代码
自定义tabBarController实现类微博tabBar
#import "SinaTabBarController.h" #import "FirstController.h" #import "SecondController.h" #import "ThirdController.h" #import "FourthController.h" #import "SinaTabBar.h" @interface SinaTabBarController () @property (nonatomic, strong) UIView *popView; @end @implementation XMGTabBarController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //添加子控制器 [self addVC:[[FirstController alloc] init] title:@"一" image:@"tabBar_First_icon" selectedImage:@"tabBar_First_icon_click"]; [self addVC:[[SecondController alloc] init] title:@"二" image:@"tabBar_second_icon" selectedImage:@"tabBar_second_icon_click"]; [self addVC:[[ThirdController alloc] init] title:@"三" image:@"tabBar_third_icon" selectedImage:@"tabBar_third_icon_click"]; [self addVC:[[FourthController alloc] init] title:@"四" image:@"tabBar_fourth_icon" selectedImage:@"tabBar_fourth_icon_click"]; SinaTabBar *sinaTabBar = [[SinaTabBar alloc] init]; //弹出 popView 事件 [sinaTabBar.pButton addTarget:self action:@selector(popToView) forControlEvents:UIControlEventTouchUpInside]; //更换 tabBar [self setValue:sinaTabBar forKeyPath:@"tabBar"]; } /** 初始化自控制器 @param title tabBarItem名 @param imageName 图片名 @param selectedImageName 选中图片名 */ - (void)addVC:(UIViewController *)vc title:(NSString *)title image:(NSString *)imageName selectedImage:(NSString *)selectedImageName { vc.tabBarItem.title = title; vc.tabBarItem.image = [UIImage imageNamed:imageName]; vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]; [self addChildViewController:nav]; } #pragma mark - 点击弹出的视图 - (void)popToView { //点击中间的按钮弹出一个视图 self.popView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)]; self.popView.backgroundColor = [UIColor whiteColor]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 40)]; label.text = @"弹出视图"; label.textColor = [UIColor blackColor]; [self.popView addSubview:label]; [self.view addSubview:self.popView]; //底部关闭按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0, kScreenHeight - 49, kScreenWidth, 49); [button setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal]; button.backgroundColor = [UIColor lightGrayColor]; [button addTarget:self action:@selector(tapToRemove) forControlEvents:UIControlEventTouchUpInside]; [self.popView addSubview:button]; } #pragma mark - 点击弹出视图上的关闭按钮 - (void)tapToRemove { //移出当前遮盖的视图 [self.popView removeFromSuperview]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
此方法直接在二级页面控制器中调用
self.hidesBottomBarWhenPushed = YES即可隐藏自定义的tabBar
方法二
不用新建继承于tabBar的子控件隐藏系统tabBar, 用和 tabBar 一样高度的View添加在 tabBar 处, 这个方法不可以用self.hidesBottomBarWhenPushed = YES方法实现跳转隐藏
代码实现
自定义tabBarController
在自定义tabBarController声明方法中添加@property (nonatomic, strong) UIView *sinaTabBar;
在.m中实现代码
#import "SinaTabBarController.h" #import "FViewController.h" #import "SViewController.h" #import "TViewController.h" #import "FOViewController.h" #define ScreenWidth [UIScreen mainScreen].bounds.size.width #define ScreenHeight [UIScreen mainScreen].bounds.size.height @interface SinaTabBarController () //中间弹出视图 @property (nonatomic, strong) UIView *popView; @end @implementation SinaTabBarController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setTabBar]; [self setPopView]; FViewController *first = [[FViewController alloc] init]; SViewController *second = [[SViewController alloc] init]; TViewController *third = [[TViewController alloc] init]; FOViewController *fourth = [[FOViewController alloc] init]; UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:first]; UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:second]; UINavigationController *thirdNav = [[UINavigationController alloc] initWithRootViewController:third]; UINavigationController *fourthNav = [[UINavigationController alloc] initWithRootViewController:fourth]; self.viewControllers = @[firstNav, secondNav, thirdNav, fourthNav]; } #pragma mark - 点击弹出视图上的关闭按钮 - (void)tapToRemove { //移出当前遮盖的视图 [self.popView removeFromSuperview]; } #pragma mark - 点击自定义的 tabBar 视图上的按钮 - (void)tapButton:(UIButton *)button { if (button.tag == 2) { [self.view addSubview:self.popView]; } else if (button.tag >= 3) { //因为有五个按钮, 而只有4个 viewController, selectedIndex 会向前移动一个 [self setSelectedIndex:button.tag - 1]; } else { [self setSelectedIndex:button.tag]; } } #pragma mark - 配置 tabBarView - (void)setTabBar { self.tabBar.hidden = YES; self.sinaTabBar = [[UIView alloc] initWithFrame:self.tabBar.frame]; self.sinaTabBar.backgroundColor = [UIColor colorWithRed:0.134 green:0.283 blue:0.344 alpha:0.195]; self.sinaTabBar.tag = 1000; [self.view addSubview:self.sinaTabBar]; for (NSInteger i = 0; i < 5; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(i * ScreenWidth / 5, 0, ScreenWidth * 0.2, self.tabBar.frame.size.height); UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"tabBar%ld", i]]; [button setImage:image forState:UIControlStateNormal]; button.tag = i; [button addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside]; [self.sinaTabBar addSubview:button]; } } #pragma mark - 初始化弹出的视图 - (void)setPopView { //点击中间的按钮弹出一个视图 self.popView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)]; self.popView.backgroundColor = [UIColor whiteColor]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 100, 40)]; label.text = @"弹出视图"; label.textColor = [UIColor blackColor]; [self.popView addSubview:label]; //底部关闭按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0, ScreenHeight - 49, ScreenWidth, 49); [button setImage:[UIImage imageNamed:@"5"] forState:UIControlStateNormal]; button.backgroundColor = [UIColor lightGrayColor]; [button addTarget:self action:@selector(tapToRemove) forControlEvents:UIControlEventTouchUpInside]; [self.popView addSubview:button]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
跳转隐藏
在二级页面控制器中调用两个视图加载方法:- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; UIView *sinaBar = [self.tabBarController.view viewWithTag:1000]; [UIView animateWithDuration:0.25 animations:^{ sinaBar.frame = CGRectMake(0, kScreenHeight, kTabBarWidth, kTabBarHeight); }]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; UIView *sinaBar = [self.tabBarController.view viewWithTag:1000]; [UIView animateWithDuration:0.25 animations:^{ sinaBar.frame = CGRectMake(0, kScreenHeight- kTabBarHeight, kTabBarWidth, kTabBarHeight); }]; }
这样会有一个隐藏的动画效果, 也可以不用, 就和系统的隐藏方法一样了
PS:
方法一中也可以在viewWill的两个方法中用偏移量解决self.tabBarcontroller.tabBar.frame = (...)
弹出的View上子控件的动画
这里推荐一个国外大牛的三方facebook/pop
动画很全面了
自己设置好初始坐标和结束坐标, 添加动画
这里就不再赘述了
相关文章推荐
- 【CF应用开发大赛】微博社交简历
- 微博回应“用户信息被出售”事件:已上报司法机关
- 我是运营,我没有假期
- [网络资讯]新浪微博新版三栏布局开放公测
- 开始我觉得新浪微博傻,现在才慢慢明白新浪微博的高瞻远瞩
- jQuery+PHP+ajax实现微博加载更多内容列表功能
- js实现仿微博滚动显示信息的效果
- nodejs开发微博实例
- iOS开发之路--微博“更多”页面
- 如何使用微博自动记录俯卧撑个数
- js实现将选中内容分享到新浪或腾讯微博
- JavaScript实现仿新浪微博大厅和腾讯微博首页滚动特效源码
- js仿微博实现统计字符和本地存储功能
- 基于js实现微信发送好友如何分享到朋友圈、微博
- 基于python编写的微博应用
- php新浪微博登录接口用法实例
- PHP+Mysql+jQuery实现发布微博程序 php篇
- Java使用新浪微博API通过账号密码方式登陆微博的实例
- Java使用新浪微博API开发微博应用的基本方法
- java实现的新浪微博分享代码实例