美文网首页
iOS学习之UITabBarController和UINavig

iOS学习之UITabBarController和UINavig

作者: 龙马君 | 来源:发表于2016-03-24 10:48 被阅读826次

效果如下:

Paste_Image.png

点击按钮:

Paste_Image.png

主要代码:
TabBarControllerViewController继承UITabBarController

@interface TabBarControllerViewController ()
{
    // 需要为每个Tab页面建立导航
    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里面增加按钮调整

- (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:@"跳转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里面实现隐藏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];
}

相关文章

网友评论

      本文标题:iOS学习之UITabBarController和UINavig

      本文链接:https://www.haomeiwen.com/subject/drrflttx.html