导航控制器和自定义导航控制器

Posted bys别

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了导航控制器和自定义导航控制器相关的知识,希望对你有一定的参考价值。

导航控制器
appDelegate
ViewController * vc = [[ViewController alloc] init];
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;

具体在ViewController中设置
    //设置标题
    self.title = @"首页你好再见哈哈杯子";
    
    //添加导航条左按钮,设置系统风格按钮
    UIBarButtonItem * leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
    //设置文字按钮
    UIBarButtonItem * rightBarButton1 = [[UIBarButtonItem alloc] initWithTitle:@"更多" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction1:)];
    
    //设置图片按钮
    UIBarButtonItem * rightBarButton2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"setting_item"] style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.rightBarButtonItems = @[rightBarButton1,rightBarButton2];
    
    //修改按钮颜色
    self.navigationController.navigationBar.tintColor = [UIColor greenColor];
    
    //设置navigationBar的颜色,不透明了。
//    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
    
    //设置navigationBar的样式,设置barTintColor功能失效
//    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    
    //设置navigationBar是否透明
//    self.navigationController.navigationBar.translucent = NO;
 
    NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:20],
                            NSForegroundColorAttributeName:[UIColor whiteColor]};
    //单独设置按钮颜色
    [self.navigationItem.leftBarButtonItem setTitleTextAttributes:dict forState:UIControlStateNormal];
    
    
    NSDictionary * titleDict = @{NSFontAttributeName:[UIFont systemFontOfSize:30],
                            NSForegroundColorAttributeName:[UIColor yellowColor]};
    //设置导航条题目
    [self.navigationController.navigationBar setTitleTextAttributes:titleDict];
    
    //设置提示
//    self.navigationItem.prompt = @"我是提示";
    
    //隐藏navigation
//    self.navigationController.navigationBarHidden = YES;
    
    //修改返回按钮
    
    UIBarButtonItem * item = [[UIBarButtonItem alloc] init];
    item.title = @"返回";
    self.navigationItem.backBarButtonItem = item;


    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"下一页" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(100, 100, 100, 100);
    [self.view addSubview:btn];
}

- (void)nextAction:(UIButton *)btn {
    
    NextViewController * nextVC = [[NextViewController alloc] init];
    [self.navigationController pushViewController:nextVC animated:YES];//下一页方法
}

自定义导航条
    //导航控制器下背景一定要设置,否则有卡顿现象


//自定义导航titleView
    UISegmentedControl * segment = [[UISegmentedControl alloc] initWithItems:@[@"消息",@"电话"]];
    segment.frame = CGRectMake(0, 0, 60, 30);
    self.navigationItem.titleView = segment;

 //设置返回按钮,但是效果不佳-颜色不太好看
//    UIBarButtonItem * backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backBtn"] style:UIBarButtonItemStylePlain target:nil action:nil];
//    self.navigationItem.backBarButtonItem = backItem;


//跳转下一页
- (void)upAction {
    //导航控制器的方法
    SecondViewController * secondVC = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondVC animated:YES];
    
    
//    ViewController * vc = [[ViewController alloc] init];
//    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
//    [self presentViewController:nav animated:YES completion:nil];

}


//    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]
//                      ;
//    [btn setImage:[UIImage imageNamed:@"backBtn"] forState:UIControlStateNormal];
//    btn.frame = CGRectMake(0, 0, 30, 30);
//    [btn addTarget:self action:@selector(returnAction:) forControlEvents:UIControlEventTouchUpInside];
//    
//    //自定义返回按钮,返回手势消失
//    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
//    
//    self.navigationItem.leftBarButtonItem = item;


BaseNavViewController.h

//appearance调用一次,作用于整个app
    
    UINavigationBar * bar = [UINavigationBar appearance];
    UIBarButtonItem * barItem = [UIBarButtonItem appearance];
    
    //设置UINavigationBar的颜色
    bar.barTintColor = [UIColor yellowColor];
    
    NSDictionary * titleDict = @{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor greenColor]
     
                                 };
    //设置UINavigationBar的题目

    [bar setTitleTextAttributes:titleDict];
    
    //设置UINavigationBar的背景图片
    [bar setBackgroundImage:[UIImage imageNamed:@"nav"] forBarMetrics:UIBarMetricsDefault];
    
    NSDictionary * titleItemDict = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor magentaColor]
                                 };
    [barItem setTitleTextAttributes:titleItemDict forState:UIControlStateNormal];

    //因为自定义了左item,手势消失,所以加上这句话,还原手势。
//    self.interactivePopGestureRecognizer.delegate = self;
    
}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    //self.viewControllers拿到所有管理的控制器,self.viewControllers.count压栈的数量
    
    if (self.viewControllers.count) {
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]
        ;
        [btn setImage:[UIImage imageNamed:@"backBtn"] forState:UIControlStateNormal];
        btn.frame = CGRectMake(0, 0, 30, 30);
        [btn addTarget:self action:@selector(returnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        //自定义返回按钮,返回手势消失
        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        
        viewController.navigationItem.leftBarButtonItem = item;
        
    }
    
    [super pushViewController:viewController animated:animated];
}


以上是关于导航控制器和自定义导航控制器的主要内容,如果未能解决你的问题,请参考以下文章

如何将自定义导航栏链接到导航控制器?

如何让自定义导航控制器为每个故事板提供相同的自定义导航栏按钮

自定义 UIView XIB 内的 iOS 导航返回按钮(无导航控制器)

导航架构组件 - 如何使用导航控制器设置/更改自定义后退或汉堡图标?

使用 Storyboards 设置自定义导航栏

自定义控制器包含和导航栏高度