框架搭建_純代碼

目錄:
1荧止、利用ViewController中間過渡
2屹电、直接設(shè)置UITabBarController的數(shù)組

比較:相對來說第2中方式較方便,設(shè)置內(nèi)容跃巡、標(biāo)題等比較清晰明確危号,第1中方式中設(shè)置標(biāo)題等內(nèi)容時容易搞混,相對第2中方式?jīng)]有較大的優(yōu)勢素邪。

1外莲、利用ViewController中間過渡

AppDelegate中代碼設(shè)置

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    ViewController *vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;
    
    return YES;
}

ViewController中代碼設(shè)置

    //初始化要使用的三個VC
    FirstVc *first = [[FirstVc alloc] init];
    SecondVc *second = [[SecondVc alloc] init];
    ThirdVc *third = [[ThirdVc alloc] init];
    
    //初始化三個nav
    UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:first];
    firstNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"111" image:nil tag:1];
    
    UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:second];
    secondNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:nil tag:1];

    UINavigationController *thirdNav = [[UINavigationController alloc] initWithRootViewController:third];
    thirdNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"333" image:nil tag:1];

    //初始化UITabBarController
    self.tabBarCont = [[UITabBarController alloc] init];
    self.tabBarCont.viewControllers = @[firstNav,secondNav,thirdNav];
    
    self.tabBarCont.selectedIndex = 0;
    self.tabBarCont.delegate = self;
    self.tabBarCont.view.frame = self.view.frame;
    
    [self.view addSubview:self.tabBarCont.view];

各個VC中的設(shè)置

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.title = @"first";//此設(shè)置改變nav的標(biāo)題和底部item的標(biāo)題
    self.navigationController.title = @"111";//item標(biāo)題
}

2、直接設(shè)置UITabBarController的數(shù)組

AppDelegate中設(shè)置

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //初始化
    FirstVc *first = [[FirstVc alloc] init];
    SecondVc *second = [[SecondVc alloc] init];
    ThirdVc *third = [[ThirdVc alloc] init];
    
    //創(chuàng)建標(biāo)簽欄控制器
    tabBarControl = [UITabBarController new];
    
    //放入標(biāo)簽欄中
    tabBarControl.viewControllers = @[first,second,third];
    
    //創(chuàng)建導(dǎo)航欄控制器兔朦,并指定他的根視圖控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tabBarControl];
    
    //添加中間的按鈕
    //缺點(diǎn):1偷线、按鈕超過bottom的部分點(diǎn)擊無響應(yīng)    2、點(diǎn)擊除按鈕外的中間部分時也展示中間item的頁面
    //3個item是可能范圍太大沽甥,如果調(diào)整item數(shù)量声邦,缺點(diǎn)2應(yīng)該影響不大。
    UIButton *btn = [[UIButton alloc] init];
    btn.backgroundColor = [UIColor redColor];
    [btn setFrame:CGRectMake(130, -12, 60, 60)];
    btn.clipsToBounds = YES;
    btn.layer.cornerRadius = 30;
    [btn addTarget:self action:@selector(clickCenterButton) forControlEvents:UIControlEventTouchUpInside];
    
    [tabBarControl.tabBar addSubview:btn];
    
    //指定應(yīng)用的跟視圖控制器
    self.window.rootViewController = nav;
    
    return YES;
}

各個VC中的設(shè)置

- (instancetype)init
{
    self = [super init];
    
    if (self)
    {
        self.title = @"111";//此處的title是item的標(biāo)題
        
        //設(shè)置圖標(biāo)的默認(rèn)圖片和選中后圖片
        self.tabBarItem.image = [[UIImage imageNamed:@"tabbar_mainframe@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_mainframeHL@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        //未讀消息數(shù)量(右上角標(biāo)識)
        [self.tabBarItem setBadgeValue:@"18"];
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.navigationItem.title = @"first";//共同使用一個navigation摆舟,要在此方法中設(shè)置名稱
    
    //設(shè)置右邊導(dǎo)航按鈕
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(tapAdd)];

    self.tabBarController.navigationItem.rightBarButtonItem = rightItem;
}

- (void)tapAdd
{
    //此處跳轉(zhuǎn)頁面操作
    //或者彈出頁面
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末亥曹,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子恨诱,更是在濱河造成了極大的恐慌媳瞪,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件照宝,死亡現(xiàn)場離奇詭異蛇受,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)厕鹃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進(jìn)店門龙巨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來笼呆,“玉大人,你說我怎么就攤上這事旨别∈模” “怎么了?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵秸弛,是天一觀的道長铭若。 經(jīng)常有香客問我,道長递览,這世上最難降的妖魔是什么叼屠? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮绞铃,結(jié)果婚禮上镜雨,老公的妹妹穿的比我還像新娘。我一直安慰自己儿捧,他們只是感情好荚坞,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著菲盾,像睡著了一般颓影。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上懒鉴,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天诡挂,我揣著相機(jī)與錄音,去河邊找鬼临谱。 笑死璃俗,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的悉默。 我是一名探鬼主播城豁,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼麦牺!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起鞭缭,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤剖膳,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后岭辣,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吱晒,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年沦童,在試婚紗的時候發(fā)現(xiàn)自己被綠了仑濒。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片叹话。...
    茶點(diǎn)故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖墩瞳,靈堂內(nèi)的尸體忽然破棺而出驼壶,到底是詐尸還是另有隱情,我是刑警寧澤喉酌,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布热凹,位于F島的核電站,受9級特大地震影響泪电,放射性物質(zhì)發(fā)生泄漏般妙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一相速、第九天 我趴在偏房一處隱蔽的房頂上張望碟渺。 院中可真熱鬧,春花似錦突诬、人聲如沸苫拍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽怯疤。三九已至,卻和暖如春催束,著一層夾襖步出監(jiān)牢的瞬間集峦,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工抠刺, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留塔淤,地道東北人。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓速妖,卻偏偏與公主長得像高蜂,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子罕容,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,960評論 2 355

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

  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多备恤,會對里面所有的內(nèi)容的引用計數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,488評論 1 14
  • 1.自定義控件 a.繼承某個控件 b.重寫initWithFrame方法可以設(shè)置一些它的屬性 c.在layouts...
    圍繞的城閱讀 3,391評論 2 4
  • 今天給大家推薦一首我特別喜歡的歌和MV 《其實我們值得幸附趺耄》露泊。 這首歌曲的歌詞是管啟根據(jù)楊丞琳與林依晨兩人的友誼所...
    鄒小芝閱讀 3,024評論 9 27
  • 1. js六大數(shù)據(jù)類型 null并非typeof出來的類型,不過由于null不可再分旅择,所以將其歸于基本數(shù)據(jù)類型之中...
    二狗的小仙女閱讀 513評論 0 0
  • 2017-1-29晴 今天早上回天津惭笑。東北下雪了,路滑不好走,但車不是很多沉噩。一路上捺宗,孩子吃吃喝喝,睡覺川蒙,我們車上聊...
    心境色彩閱讀 206評論 0 0