自定義TabBar

//在AppDelegate中寫代碼

//把main.storyBoard刪除后,不再加載,這里需要手動加載window

self.window= [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.window.backgroundColor= [UIColor whiteColor];

[self.window makeKeyAndVisible];

//在不指定根控制器的情況下,運(yùn)行會崩潰

MyTabBarController *tabBarController = [[MyTabBarControlleralloc] init];

self.window.rootViewController = tabBarController;


//繼承于UITabBarControlle進(jìn)行自定義

[super viewDidLoad];

//加載子視圖控制器

[self loadViewControllers];

//tabBar的層次構(gòu)成

//背景視圖(下)

[self loadCustomTabBar];

//選中視圖(中)

//按鈕(上)

//加載自定義tabBar

- (void)loadCustomTabBar {

//創(chuàng)建tabBar的背景視圖

_bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,HEIGHT- 50,WIDTH, 50)];

_bgView.image = [UIImage imageNamed:@"tabBar"];

//用戶交互性

_bgView.userInteractionEnabled=YES;

[self.view addSubview:_bgView];

//創(chuàng)建選中視圖

_selectedView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"select"]];

CGFloat eachWidth = WIDTH/_itemsCount;

//默認(rèn)顯示第一個

_selectedView.center =CGPointMake(eachWidth / 2.0, 25);

_selectedView.bounds = CGRectMake(0, 0, 53, 45);

[_bgView addSubview:_selectedView];

//按鈕

for(inti = 0; i <_itemsCount; i ++) {

UIButton *item = [UIButtonbuttonWithType:UIButtonTypeCustom];

item.center =CGPointMake(eachWidth / 2.0 + (eachWidth * i), 25);

item.bounds =CGRectMake(0, 0, 42, 40);

[item setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i + 1]] forState:UIControlStateNormal];

[item addTarget:selfaction:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];

item.tag= i + 1;

[_bgView addSubview:item];

}

}

- (void)itemClick:(UIButton*)sender {

//selectedIndex設(shè)置tabBarController選中的子視圖控制器索引

self.selectedIndex = sender.tag-1;

//改變選擇視圖的位置

[UIView animateWithDuration:0.3animations:^{

_selectedView.center = sender.center;

}];

// ???if (sender.tag == 3) {

// ???????[self hidderTabBar];

// ???}

}

//顯示

- (void)showTabBar {

[UIView animateWithDuration:0.3 animations:^{

_bgView.frame =CGRectMake(0,HEIGHT- 50 ,WIDTH, 50);

}];

}

- (void)hidderTabBar {

[UIView animateWithDuration:0.3animations:^{

_bgView.frame=CGRectMake(-WIDTH,HEIGHT- 50 ,WIDTH, 50);

}];

}

- (void)loadViewControllers {

ViewController *vc0 = [[ViewController alloc] init];

ViewController1 *vc1 = [[ViewController1 alloc] init];

ViewController2 *vc2 = [[ViewController2 alloc] init];

ViewController3 *vc3 = [[ViewController3 alloc] init];

ViewController4 *vc4 = [[ViewController4 alloc] init];

NSArray *viewControllers = @[vc0,vc1,vc2,vc3,vc4];

_itemsCount = viewControllers.count;

//把視圖控制器數(shù)組賦值給自身的viewControllers屬性

self.viewControllers = viewControllers;

//隱藏系統(tǒng)的tabBar

self.tabBar.hidden =YES;

}

{

//tabBar背景視圖

UIImageView *_bgView;

//選中視圖

UIImageView *_selectedView;

//子視圖控制器個數(shù)

NSInteger _itemsCount;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末日戈,一起剝皮案震驚了整個濱河市邓夕,隨后出現(xiàn)的幾起案子粘我,更是在濱河造成了極大的恐慌鲁沥,老刑警劉巖配乱,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件排抬,死亡現(xiàn)場離奇詭異两踏,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)艰垂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進(jìn)店門泡仗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人猜憎,你說我怎么就攤上這事娩怎。” “怎么了胰柑?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵截亦,是天一觀的道長爬泥。 經(jīng)常有香客問我,道長崩瓤,這世上最難降的妖魔是什么袍啡? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮却桶,結(jié)果婚禮上境输,老公的妹妹穿的比我還像新娘。我一直安慰自己颖系,他們只是感情好嗅剖,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著嘁扼,像睡著了一般信粮。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上偷拔,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天蒋院,我揣著相機(jī)與錄音,去河邊找鬼莲绰。 笑死,一個胖子當(dāng)著我的面吹牛姑丑,可吹牛的內(nèi)容都是我干的蛤签。 我是一名探鬼主播,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼栅哀,長吁一口氣:“原來是場噩夢啊……” “哼震肮!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起留拾,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤戳晌,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后痴柔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體沦偎,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年咳蔚,在試婚紗的時候發(fā)現(xiàn)自己被綠了豪嚎。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡谈火,死狀恐怖侈询,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情糯耍,我是刑警寧澤扔字,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布囊嘉,位于F島的核電站,受9級特大地震影響革为,放射性物質(zhì)發(fā)生泄漏扭粱。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一篷角、第九天 我趴在偏房一處隱蔽的房頂上張望焊刹。 院中可真熱鬧,春花似錦恳蹲、人聲如沸虐块。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽贺奠。三九已至,卻和暖如春错忱,著一層夾襖步出監(jiān)牢的瞬間儡率,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工以清, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留儿普,地道東北人。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓掷倔,卻偏偏與公主長得像眉孩,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子勒葱,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評論 2 361

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