框架搭建_純代碼

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

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

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è)置

    //初始化要使用的三個(gè)VC
    FirstVc *first = [[FirstVc alloc] init];
    SecondVc *second = [[SecondVc alloc] init];
    ThirdVc *third = [[ThirdVc alloc] init];
    
    //初始化三個(gè)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];

各個(gè)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)擊除按鈕外的中間部分時(shí)也展示中間item的頁面
    //3個(gè)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;
}

各個(gè)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)識(shí))
        [self.tabBarItem setBadgeValue:@"18"];
    }
    
    return self;
}

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

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.navigationItem.title = @"first";//共同使用一個(gè)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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容