自定義UITabbarController(含突出按鈕)

1.思路

? ? ? ?最近項(xiàng)目中需要在tabbar中間做一個(gè)突出按鈕,查了很多資料也沒看到,于是按照自己的想法寫了個(gè),基本算是實(shí)現(xiàn)了。具體思路是給tabbbar中間添加多一個(gè)item ,然后在中間添加一個(gè)按鈕就行了,把按鈕暴露出來,然后做跳轉(zhuǎn)的時(shí)候直接隱藏按鈕。

2.自定義步驟

? 新建一個(gè)類繼承UITabBarController,再把每個(gè)包含UIViewController的 UINavigationController添加到item。可能你覺得說的不清楚,看代碼會(huì)明白些:

.h文件?

@interface MyTabBarController : UITabBarController

@property(nonatomic,strong)UIButton *centerButton;

@property(nonatomic,strong)UILabel *centerLabel;

@end


.m文件部分重點(diǎn)代碼

#pragma mark-設(shè)置導(dǎo)航欄和添加控制器

-(void)setVC{

self.tabBar.tintColor = [UIColor whiteColor];

// 背景更改為白色

UIView *view = [[UIView alloc] init];

view.backgroundColor = [UIColor whiteColor];

view.frame = self.tabBar.bounds;

[self.tabBar insertSubview:view atIndex:0];

//這里是每個(gè)控制器

NearByViewController *nearbyVC = [[NearByViewController alloc] init];

[self.navigationController pushViewController:nearbyVC animated:YES];

[self setupChildViewController:nearbyVC title:@"附近" image:@"nearby_unselect" selectedImage:@"nearby"];

DynamicViewController *dynamicVC = [[DynamicViewController alloc]init];

[self.navigationController pushViewController:dynamicVC animated:YES];

[self setupChildViewController:dynamicVC title:@"動(dòng)態(tài)" image:@"dynamic_unselect" selectedImage:@"dynamic"];

UIViewController *vc = [[UIViewController alloc]init];

[self setupChildViewController:vc title:@"" image:@"" selectedImage:@""];

MessageViewController *messageVC = [[MessageViewController alloc]init];

[self setupChildViewController:messageVC title:@"消息" image:@"news_unselect" selectedImage:@"news"];

MineViewController *mineVC = [[MineViewController alloc]init];

[self setupChildViewController:mineVC title:@"我的" image:@"mine_unselect" selectedImage:@"mine"];

[self addMiddleBtn];

self.delegate = self;

//設(shè)置陰影

self.tabBar.layer.shadowColor = RGB(159, 159, 159).CGColor;//shadowColor陰影顏色

self.tabBar.layer.shadowOffset = CGSizeMake(2.0f,5.0f);//shadowOffset陰影偏移x,y向(上/下)偏移(-/+)2

self.tabBar.layer.shadowOpacity = 0.8f;//陰影透明度,默認(rèn)0

self.tabBar.layer.shadowRadius = 6.0f;//陰影半徑

//去掉黑線

self.tabBar.barStyle = UIBarStyleBlack;

}

//當(dāng)控制器為中間的控制器時(shí),返回no

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

if(viewController == [tabBarController.viewControllers objectAtIndex:2]){

return NO;

}else {

return YES;

}

}


#pragma mark-添加中間突出按鈕 和標(biāo)題

-(void)addMiddleBtn{

//創(chuàng)建一個(gè)自定義button

_centerButton = [UIButton buttonWithType:UIButtonTypeCustom];

//初始化button的背景圖片

UIImage *centerButtonImg = [UIImage imageNamed:@"pp"];

//設(shè)置button的frame

_centerButton.frame = CGRectMake(0, 0, centerButtonImg.size.width, centerButtonImg.size.height);

//設(shè)置button的背景圖片

[_centerButton setBackgroundImage:centerButtonImg forState:UIControlStateNormal];

CGFloat imageHeight = centerButtonImg.size.height;

//NSLog(@"imageHeight=%2.f,barHeight=%.2f",imageHeight,barHeight);

CGFloat delta = imageHeight/3.0+1;

CGPoint center = self.tabBar.center;

center.y = center.y - delta;

_centerButton.center = center;

//點(diǎn)擊btn

[_centerButton addTarget:self action:@selector(issueBgViewShow) forControlEvents:UIControlEventTouchUpInside];

//將centerButton加入到tabBarController中

[self.view addSubview:_centerButton];

_centerLabel = [[UILabel alloc]init];

_centerLabel.text = @"發(fā)現(xiàn)";

_centerLabel.textAlignment = NSTextAlignmentCenter;

_centerLabel.textColor = [UIColor grayColor];

_centerLabel.font = [UIFont systemFontOfSize:10];

[self.view addSubview:_centerLabel];

[_centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(_centerButton.mas_bottom).offset(3);

make.centerX.mas_equalTo(_centerButton);

make.height.mas_equalTo(@15);

}];

}


/**

* 初始化子控制器

*/

-(void)setupChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString*) selectedImage{

//? ? vc.navigationItem.title = title;

//? ? [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : RGB(255, 0,79)} forState:UIControlStateNormal];

[vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : RGB(255, 0,79)} forState:UIControlStateSelected];

vc.tabBarItem.title = title;

vc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

//? ? UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];

MyNavViewController *nav = [[MyNavViewController alloc]initWithRootViewController:vc];

[self addChildViewController:nav];

}

/**

*? 點(diǎn)擊tabbar觸發(fā)的方法

*/

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

NSInteger index = [self.tabBar.items indexOfObject:item];

}


注意:因?yàn)橹虚g按鈕是不包含導(dǎo)航欄控制器的,所以如果點(diǎn)擊中間按鈕跳轉(zhuǎn)到其他頁(yè)面,只能用模態(tài)跳轉(zhuǎn),寫的文字比較少,有什么不懂的評(píng)論,反正我也不一定知道。最后貼上效果圖


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,001評(píng)論 6 537
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,786評(píng)論 3 423
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,986評(píng)論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,204評(píng)論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,964評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,354評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,410評(píng)論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,554評(píng)論 0 289
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,106評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,918評(píng)論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,093評(píng)論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,648評(píng)論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,342評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,755評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,009評(píng)論 1 289
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,839評(píng)論 3 395
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,107評(píng)論 2 375

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

  • 前言 很多時(shí)候,系統(tǒng)原生的 UITabBar 并不能滿足我們的需求,譬如我們想要給圖標(biāo)做動(dòng)態(tài)的改變,或者比較炫一點(diǎn)...
    四月_Hsu閱讀 5,076評(píng)論 1 6
  • 開發(fā)是一個(gè)學(xué)習(xí)的過程,當(dāng)你在項(xiàng)目中遇到難點(diǎn)的時(shí)候,第一個(gè)想到的應(yīng)該是Google,百度...我總是拿這樣一句話來形...
    Senior丶閱讀 4,808評(píng)論 35 67
  • 簡(jiǎn)介 UITabBar是iOS App中經(jīng)常使用的系統(tǒng)控件,比如知名App:新浪微博,微信,騰訊QQ等。在實(shí)際的項(xiàng)...
    清蘂翅膀的技術(shù)閱讀 1,885評(píng)論 0 6
  • 創(chuàng)建工程添加第三方RESideMenu添加PCH文件Starry.pch在程序Build Settings 的Pr...
    Wang99閱讀 335評(píng)論 0 0
  • 龍龑閱讀 183評(píng)論 0 2