两种方法实现类微博tabBar,并跳转页面隐藏

来源:互联网 发布:js微信短网址生成api 编辑:程序博客网 时间:2024/05/14 09:02

方法一

用自定义tabBar替换系统tabBar, 然后调用self.hidesBottomBarWhenPushed = YES直接隐藏


代码实现

自定义tabBar

首先自定义一个tabBar继承于UItabBar

tabBar.png
在自定义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 *)imageNameselectedImage:(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
动画很全面了
自己设置好初始坐标和结束坐标, 添加动画
这里就不再赘述了

0 0
原创粉丝点击