兩種方法實(shí)現(xiàn)類(lèi)微博tabBar, 跳轉(zhuǎn)頁(yè)面并隱藏

方法一

用自定義tabBar替換系統(tǒng)tabBar, 然后調(diào)用self.hidesBottomBarWhenPushed = YES直接隱藏


代碼實(shí)現(xiàn)

自定義tabBar

首先自定義一個(gè)tabBar繼承于UItabBar

tabBar.png

在自定義tabBar的.h文件中輸入
@property (nonatomic, weak) UIButton *pButton;
在.m文件開(kāi)始布局自定義tabBar

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
    
        //添加發(fā)布按鈕
        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];
    
    //設(shè)置發(fā)布按鈕的 frame
    self.pButton.size = self.publishButton.currentBackgroundImage.size;
    self.pButton.center = CGPointMake(self.width * 0.5, self.height * 0.5);
    
    //設(shè)置其他 UITabBarButton 的 frame
    CGFloat buttonY = 0;
    CGFloat buttonW = self.width / 5;
    CGFloat buttonH = self.height;
    
    NSInteger buttonIndex = 0;
    
    for (UIView *button in self.subviews) {
        
        /**
         計(jì)算按鈕 X 值, 為了空出中間的距離
         從第二個(gè)tabBarButton起, 第三個(gè)按鈕為我們自定義的按鈕, 第四個(gè)第五個(gè)為系統(tǒng)的按鈕
         index從0起, 那么當(dāng)buttonIndex > 1
         也就是我們看到的第四個(gè)按鈕起
         X值就為按鈕寬度 * (按鈕下標(biāo) + 1)
         */
        CGFloat buttonX = 0;
        
        if (buttonIndex > 1) {
            buttonX = buttonW * (buttonIndex + 1);
        } else {
            buttonX = buttonW * buttonIndex;
        }
        
        //如果按鈕不是tabBarButton, 那么不管它, 繼續(xù)
        if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            continue;
        }
        
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        
        //增加索引
        buttonIndex++;
    }
}

自定義tabBarController

在tabBarController中用自定義tabBar替換系統(tǒng)自身tabBar
注意:

self.tabBar = sinaTabBar;

行不通的, 大家可以試試, 因?yàn)閟elf.tabBar是私有屬性.
但是可以用****KVC****替換

SinaTabBar *sinaTabBar = [[SinaTabBar alloc] init];
    
[sinaTabBar.pButton addTarget:self
                             action:@selector(popToView)
                   forControlEvents:UIControlEventTouchUpInside];
    
    //替換系統(tǒng)tabBar為自定義的
[self setValue:sinaTabBar forKeyPath:@"tabBar"];

完整代碼

自定義tabBarController實(shí)現(xiàn)類(lèi)微博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 - 點(diǎn)擊彈出的視圖
- (void)popToView {

    //點(diǎn)擊中間的按鈕彈出一個(gè)視圖
    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];
    
    //底部關(guān)閉按鈕
    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 - 點(diǎn)擊彈出視圖上的關(guān)閉按鈕
- (void)tapToRemove {
    //移出當(dāng)前遮蓋的視圖
    [self.popView removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

此方法直接在二級(jí)頁(yè)面控制器中調(diào)用self.hidesBottomBarWhenPushed = YES即可隱藏自定義的tabBar


方法二

不用新建繼承于tabBar的子控件
隱藏系統(tǒng)tabBar, 用和 tabBar 一樣高度的View添加在 tabBar 處, 這個(gè)方法不可以用self.hidesBottomBarWhenPushed = YES方法實(shí)現(xiàn)跳轉(zhuǎn)隱藏


代碼實(shí)現(xiàn)

自定義tabBarController

在自定義tabBarController聲明方法中添加
@property (nonatomic, strong) UIView *sinaTabBar;
在.m中實(shí)現(xiàn)代碼

#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 - 點(diǎn)擊彈出視圖上的關(guān)閉按鈕
- (void)tapToRemove {
    //移出當(dāng)前遮蓋的視圖
    [self.popView removeFromSuperview];
}

#pragma mark - 點(diǎn)擊自定義的 tabBar 視圖上的按鈕
- (void)tapButton:(UIButton *)button {
    
    if (button.tag == 2) {
        [self.view addSubview:self.popView];
    } else if (button.tag >= 3) {
        //因?yàn)橛形鍌€(gè)按鈕, 而只有4個(gè) viewController, selectedIndex 會(huì)向前移動(dòng)一個(gè)
        [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 {
    
    //點(diǎn)擊中間的按鈕彈出一個(gè)視圖
    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];
    
    //底部關(guān)閉按鈕
    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.
}

跳轉(zhuǎn)隱藏

在二級(jí)頁(yè)面控制器中調(diào)用兩個(gè)視圖加載方法:

- (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);
    }];
}

這樣會(huì)有一個(gè)隱藏的動(dòng)畫(huà)效果, 也可以不用, 就和系統(tǒng)的隱藏方法一樣了


PS:

方法一中也可以在viewWill的兩個(gè)方法中用偏移量解決
self.tabBarcontroller.tabBar.frame = (...)


彈出的View上子控件的動(dòng)畫(huà)

這里推薦一個(gè)國(guó)外大牛的三方
facebook/pop
動(dòng)畫(huà)很全面了
自己設(shè)置好初始坐標(biāo)和結(jié)束坐標(biāo), 添加動(dòng)畫(huà)
這里就不再贅述了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末露筒,一起剝皮案震驚了整個(gè)濱河市蛆楞,隨后出現(xiàn)的幾起案子却舀,更是在濱河造成了極大的恐慌垛膝,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件第股,死亡現(xiàn)場(chǎng)離奇詭異应民,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)炸茧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)瑞妇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)稿静,“玉大人梭冠,你說(shuō)我怎么就攤上這事「谋福” “怎么了控漠?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,345評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)悬钳。 經(jīng)常有香客問(wèn)我盐捷,道長(zhǎng),這世上最難降的妖魔是什么默勾? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,851評(píng)論 1 295
  • 正文 為了忘掉前任碉渡,我火速辦了婚禮,結(jié)果婚禮上母剥,老公的妹妹穿的比我還像新娘滞诺。我一直安慰自己,他們只是感情好环疼,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布习霹。 她就那樣靜靜地躺著,像睡著了一般炫隶。 火紅的嫁衣襯著肌膚如雪淋叶。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,688評(píng)論 1 305
  • 那天伪阶,我揣著相機(jī)與錄音煞檩,去河邊找鬼处嫌。 笑死,一個(gè)胖子當(dāng)著我的面吹牛斟湃,可吹牛的內(nèi)容都是我干的锰霜。 我是一名探鬼主播,決...
    沈念sama閱讀 40,414評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼桐早,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼癣缅!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起哄酝,我...
    開(kāi)封第一講書(shū)人閱讀 39,319評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤友存,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后陶衅,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體屡立,經(jīng)...
    沈念sama閱讀 45,775評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年搀军,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了膨俐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,096評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡罩句,死狀恐怖焚刺,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情门烂,我是刑警寧澤乳愉,帶...
    沈念sama閱讀 35,789評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站屯远,受9級(jí)特大地震影響蔓姚,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜慨丐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評(píng)論 3 331
  • 文/蒙蒙 一坡脐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧房揭,春花似錦备闲、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,993評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至伶唯,卻和暖如春觉既,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,107評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工瞪讼, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留钧椰,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,308評(píng)論 3 372
  • 正文 我出身青樓符欠,卻偏偏與公主長(zhǎng)得像嫡霞,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子希柿,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容