码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-03-23 10:14:55      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

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


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

标签:

原文地址:http://www.cnblogs.com/hlgbys/p/5309846.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!