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

导航控制器
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];
}


转载于:https://www.cnblogs.com/hlgbys/p/5309846.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值