iOS中的UINavigationController(导航控制器)

先在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,点击跳到下一个界面app

#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,点击跳回上一个页面、或者任意视图,或者直接返回主视图ide

#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起点)spa

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

 

 

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

//去掉子视图(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];

第二种方法:orm

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

 

清除滑动时 cell 中间的缝隙图片

//清除滑动时中间的缝隙
    choiceTableView.separatorColor=[UIColor clearColor];
相关文章
相关标签/搜索