iOS-在UITabBarController上添加中間按鈕

有時候我們需要在UITabBarController中間添加一個按鈕桑阶,并且不需要讓他控制VC。我們只需要簡單幾步就可以實現(xiàn)此效果勾邦。

一:繼承UITabBarController

@interface NNTabBarController ()
@property (nonatomic, weak) UIButton *composeButton;

@end

@implementation NNTabBarController

#pragma mark - 統(tǒng)一設(shè)置所有 UITabBarItem 的文字屬性
+ (void)initialize {
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:255/255.0 green:125/255.0 blue:0 alpha:1];
    
    UITabBarItem *items = [UITabBarItem appearance];
    [items setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [items setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}

二:加載vc和中間按鈕

#pragma mark - 初始化
- (instancetype)init {
    if (self = [super init]) {
        [self addChildViewControllers];
        [self addComposeButton];
        
    }
    return self;
}

#pragma mark - 添加所有子控制器
- (void)addChildViewControllers {
    [self addChildViewControllerClassName:@"NNHomePageController" title:@"首頁" imageName:@"tabbar_home"];
    [self addChildViewControllerClassName:@"NNFoundController" title:@"發(fā)現(xiàn)" imageName:@"tabbar_discover"];
    [self addChildViewController: [[UIViewController alloc] init]];
    [self addChildViewControllerClassName:@"NNMessageController" title:@"消息" imageName:@"tabbar_message_center"];
    [self addChildViewControllerClassName:@"NNMyController" title:@"我的" imageName:@"tabbar_profile"];
}

#pragma mark - 設(shè)置所有子控制器
- (void)addChildViewControllerClassName:(NSString *)className title:(NSString *)title imageName:(NSString *)imageName
{
    UIViewController *viewController = [[NSClassFromString(className) alloc] init];
    NNNavigationController *nav = [[NNNavigationController alloc] initWithRootViewController:viewController];
    viewController.title = title;
    viewController.view.backgroundColor = [UIColor whiteColor];
    viewController.tabBarItem.image = [UIImage imageNamed: imageName];
    viewController.tabBarItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted", imageName]];
    [self addChildViewController:nav];
}

#pragma mark - 添加中間的按鈕
- (void)addComposeButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"tabBar_publish_icon_highlighted"] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(composeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.composeButton = button;
    [self.tabBar addSubview:button];
    [self.tabBar bringSubviewToFront:button];
    CGFloat width = self.tabBar.bounds.size.width / self.childViewControllers.count - 2;
    self.tabBar.tintColor = [UIColor colorWithRed:68/255.0 green:173/255.0 blue:159/255.0 alpha:1];
    button.frame = CGRectInset(self.tabBar.bounds, 2 * width, 0);
}

#pragma mark - 點擊寫文章按鈕
- (void)composeButtonClick:(UIButton *)button {
    NSLog(@"\n點擊了寫文章按鈕");
}

三:在視圖剛要出現(xiàn)的時候講將中間按鈕置前

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 不置前點擊中間按鈕會導(dǎo)致黑屏
    [self.tabBar bringSubviewToFront:self.composeButton];
}

end


效果圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蚣录,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子眷篇,更是在濱河造成了極大的恐慌萎河,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件铅歼,死亡現(xiàn)場離奇詭異,居然都是意外死亡换可,警方通過查閱死者的電腦和手機(jī)椎椰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來沾鳄,“玉大人慨飘,你說我怎么就攤上這事。” “怎么了瓤的?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵休弃,是天一觀的道長。 經(jīng)常有香客問我圈膏,道長塔猾,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任稽坤,我火速辦了婚禮丈甸,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘尿褪。我一直安慰自己睦擂,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布杖玲。 她就那樣靜靜地躺著顿仇,像睡著了一般。 火紅的嫁衣襯著肌膚如雪摆马。 梳的紋絲不亂的頭發(fā)上臼闻,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天,我揣著相機(jī)與錄音今膊,去河邊找鬼些阅。 笑死凭豪,一個胖子當(dāng)著我的面吹牛巨缘,可吹牛的內(nèi)容都是我干的寞蚌。 我是一名探鬼主播读存,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼润绎,長吁一口氣:“原來是場噩夢啊……” “哼峻凫!你這毒婦竟也來了炊汹?” 一聲冷哼從身側(cè)響起篡撵,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤褐着,失蹤者是張志新(化名)和其女友劉穎坷澡,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體含蓉,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡频敛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了馅扣。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片斟赚。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖差油,靈堂內(nèi)的尸體忽然破棺而出拗军,到底是詐尸還是另有隱情任洞,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布发侵,位于F島的核電站交掏,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏刃鳄。R本人自食惡果不足惜盅弛,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望铲汪。 院中可真熱鬧熊尉,春花似錦、人聲如沸掌腰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽齿梁。三九已至催植,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間勺择,已是汗流浹背创南。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留省核,地道東北人稿辙。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像气忠,于是被迫代替她去往敵國和親邻储。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,927評論 2 355

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