博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS中的UINavigationController(导航控制器)
阅读量:7123 次
发布时间:2019-06-28

本文共 7164 字,大约阅读时间需要 23 分钟。

hot3.png

先在AppDelegate类里面添加窗口和导航控制器

#import "AppDelegate.h"!!!切记:引入别的视图的.h头文件#import "ViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    //创建Window    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    self.window.backgroundColor = [UIColor redColor];    //让当前的Window成为主窗口    [self.window makeKeyAndVisible];        //设置Window的根视图    ViewController *a = [[ViewController alloc]init];            //1.创建一个导航控制器    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:a];    //2.设置导航控制器为window的根视图    self.window.rootViewController=nav;        //给导航控制器着色    nav.navigationBar.barTintColor = [UIColor yellowColor];        //给导航栏添加图片    [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"qidong.jpg"] forBarMetrics:UIBarMetricsDefault];        //设置导航的透明度    [nav.navigationBar setBarStyle:UIBarStyleBlack];    nav.navigationBar.translucent = YES;            return YES;}

 

 

接着在第一个视图里面设置一个Button,点击跳到下一个界面,里面有导航栏的各种设置

#import "ViewController.h"!!!切记:引入别的视图的.h头文件#import "TwoViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        //新建一个按钮    UIButton *myButtonOne = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];    myButtonOne.backgroundColor = [UIColor blueColor];    [myButtonOne addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:myButtonOne];        //设置当前界面的颜色    self.view.backgroundColor = [UIColor orangeColor];        //设置导航栏的标题//    self.navigationItem.title = @"微信";        //自定义标题(也就是导航栏里面中间的标题可以点击,所以是个Button)    UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];    //设置标题的大小    titleButton.frame = CGRectMake(0, 0, 100, 40);    //设置标题文字的内容    [titleButton setTitle:@"我可以点击" forState:UIControlStateNormal];    //设置标题文字的颜色    [titleButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];    //设置标题文字的字体和大小    titleButton.titleLabel.font = [UIFont systemFontOfSize:20];    //给标题文字(其实是个Button)添加触动方法    [titleButton addTarget:self action:@selector(xixi:) forControlEvents:UIControlEventTouchUpInside];    //设置标题文字的底色    [titleButton setBackgroundColor:[UIColor redColor]];    //将标题文字(其实是个Button)赋给导航栏的标题    self.navigationItem.titleView =titleButton;            //设置左右两边的按钮        //设置左边的按钮//    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"左边" style:UIBarButtonItemStylePlain target:self action:@selector(selfhaha:)];//    self.navigationItem.leftBarButtonItem = left;        //设置右边的按钮    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"右边" style:UIBarButtonItemStylePlain target:self action:@selector(righthaha:)];        //设置右边第二个按钮    UIBarButtonItem *rightTwo = [[UIBarButtonItem alloc]initWithTitle:@"右二" style:UIBarButtonItemStylePlain target:self action:@selector(rightTwohaha:)];        //设置右边第三个按钮    UIBarButtonItem *rightThree = [[UIBarButtonItem alloc]initWithTitle:@"右三" style:UIBarButtonItemStylePlain target:self action:@selector(rightThreehaha:)];    //如果一次有很多按钮的话,要用数组来设置    self.navigationItem.rightBarButtonItems = @[right,rightTwo,rightThree];            //将左边的按钮换成图片    UIBarButtonItem *leftImage = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"666"] style:UIBarButtonItemStylePlain target:self action:@selector(leftImagehaha:)];    self.navigationItem.leftBarButtonItem = leftImage;                    }//按钮的方法-(void)haha:(id)a{        //push到下一个界面    TwoViewController *x = [[TwoViewController alloc]init];    [self.navigationController pushViewController:x animated:YES];        }-(void)xixi:(id)a{    NSLog(@"我是中间的标题,我被点击了");}-(void)selfhaha:(id)a{    NSLog(@"我是左边");}-(void)righthaha:(id)a{    NSLog(@"我是右边");}-(void)rightTwohaha:(id)a{    NSLog(@"我是右二");}-(void)rightThreehaha:(id)a{    NSLog(@"我是右三");}-(void)leftImagehaha:(id)a{    NSLog(@"我是左边的图片");}

 

 

接着在第二个视图里面设置一个Button,点击跳到下一个界面

#import "TwoViewController.h"//!!!切记:引入别的视图的.h头文件#import "ThreeViewController.h"@interface TwoViewController ()@end@implementation TwoViewController- (void)viewDidLoad {    [super viewDidLoad];    //设置界面颜色    self.view.backgroundColor = [UIColor purpleColor];        //新建一个按钮    UIButton *myButtonOne = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];    myButtonOne.backgroundColor = [UIColor blueColor];    [myButtonOne addTarget:self action:@selector(haha2:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:myButtonOne];    }//按钮的方法-(void)haha2:(id)a{    //跳到指定的页面    ThreeViewController *x = [[ThreeViewController alloc]init];    [self.navigationController pushViewController:x animated:YES];    }

 

 

接着在第三个视图里面设置一个Button,点击跳回上一个页面、或者任意视图,或者直接返回主视图

#import "ThreeViewController.h"//!!!切记:引入别的视图的.h头文件#import "ViewController.h"@interface ThreeViewController ()@end@implementation ThreeViewController- (void)viewDidLoad {    [super viewDidLoad];        //设置界面的颜色    self.view.backgroundColor = [UIColor greenColor];        //新建一个按钮    UIButton *myButtonOne = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];    myButtonOne.backgroundColor = [UIColor blueColor];    [myButtonOne addTarget:self action:@selector(haha3:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:myButtonOne];    }//按钮的方法-(void)haha3:(id)a{    //!!!切记:引入别的视图的.h头文件    //跳到最根的视图    [self.navigationController popToRootViewControllerAnimated:YES];        //返回上一级视图    [self.navigationController popViewControllerAnimated:YES];        //跳到任意指定视图    for (UIViewController *temp in self.navigationController.viewControllers) {                if ([temp isKindOfClass:[ViewController class]]) {            [self.navigationController popToViewController:temp animated:YES];        }    }        }

 

 

直接设置导航栏上的标题字体和颜色

//直接设置导航栏上的标题字体和颜色    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],          NSForegroundColorAttributeName:[UIColor blackColor]}];

 

 

在当前页面隐藏导航栏,并设置透明度(从导航栏下面作为 Y轴 的0起点)

//每次进来都会加载一次- (void)viewWillAppear:(BOOL)animated{        //取消隐藏导航栏(如果要隐藏,改为yes就行了)    self.navigationController.navigationBarHidden = NO;        //设置导航栏透明度    self.navigationController.navigationBar.translucent = NO; }

 

 

去掉push到的视图中 自带的返回箭头< 右边的文字,并改变箭头 < 的颜色

//去掉子视图(push到的视图)中的返回箭头 < 右边的文字    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];        self.navigationItem.backBarButtonItem = item;            //改变上面箭头的颜色(会全部都改变)    [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];    //左边返回箭头颜色(改变左边返回箭头颜色)    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

第二种方法:

//去掉PUSH后的back文字(第二种方法)    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)                                                         forBarMetrics:UIBarMetricsDefault];

 

清除滑动时 cell 中间的缝隙

//清除滑动时中间的缝隙    choiceTableView.separatorColor=[UIColor clearColor];

 

 

转载于:https://my.oschina.net/LBBB/blog/655437

你可能感兴趣的文章
html5 图片热点area,map的用法
查看>>
Java集合框架知多少——干货!!!
查看>>
P2030 - 【BJOI2006】狼抓兔子
查看>>
【随想】关于图灵机
查看>>
echarts 通过ajax实现动态数据加载
查看>>
结构化方法与面向对象方法之比较
查看>>
Pig调试环境
查看>>
Python连接MySQL数据库
查看>>
BZOJ2815:[ZJOI2012]灾难(拓扑排序,LCA)
查看>>
[转] js对象监听实现
查看>>
【leetcode】714. Best Time to Buy and Sell Stock with Transaction Fee
查看>>
mongoDB 3.0 安全权限访问控制
查看>>
电子数字 网易游戏在线笔试 第一题 hihocoder
查看>>
Java 中nextLine()方法没有执行直接跳过解决办法
查看>>
重写和重载
查看>>
本表收录的字符的Unicode编码范围为19968至40869
查看>>
PHP多次调用Mysql存储过程报错解决办法
查看>>
leetcode-680-Valid Palindrome II
查看>>
php用curl获取远端网页内容
查看>>
selenium+python谷歌驱动配置
查看>>