iOS學(xué)習(xí)之UITabBarController和UINavigationController組合

效果如下:

Paste_Image.png

點擊按鈕:

Paste_Image.png

主要代碼:
TabBarControllerViewController繼承UITabBarController

@interface TabBarControllerViewController ()
{
    // 需要為每個Tab頁面建立導(dǎo)航
    UINavigationController* nav;
    UINavigationController* nav2;
}
@end

@implementation TabBarControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    ViewController *c1 = [[ViewController alloc] init];
    c1.view.backgroundColor = [UIColor redColor];
    nav = [[UINavigationController alloc] initWithRootViewController:c1];
    nav.tabBarItem.title = @"頁面1";
    nav.hidesBottomBarWhenPushed = YES;

    UIViewController *c2 = [[UIViewController alloc] init];
    c2.view.backgroundColor = [UIColor greenColor];
    
    nav2 = [[UINavigationController alloc] initWithRootViewController:c2];
    nav2.tabBarItem.title = @"頁面2";

    // 注意,這里時nav和nav2;
    self.viewControllers = @[nav, nav2];
}

ViewController.m里面增加按鈕調(diào)整

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 80, 100, 30)];
    [button setTitle:@"跳轉(zhuǎn)Detail" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor blueColor];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

-(void)buttonClick:(id)sender{
    DetailViewController *detail = [[DetailViewController alloc] init];
    [self.navigationController pushViewController: detail animated:TRUE];
}

DetailViewController.m里面實現(xiàn)隱藏TabBar


-(instancetype)init{
    // 使底部的TabBar隱藏
    self = [super init];
    if(self){
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 100, 30)];
    label.text = @"detailViewCOntroller";
    [self.view addSubview:label];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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