iOS入门-24导航栏进阶

概述

  • 演示一下在不同的设置情况下导航栏的状态
  • 导航控制器控制视图控制器切换

示例

  • 导航栏透明度属性,导航栏底色和文字颜色;
  • 导航栏视图控制器管理视图控制器(页面跳转);

两个示例代码都放在一起了,第一个示例放在VCRoot.m视图控制器中,仔细看注释

第一个示例:导航栏透明度属性,导航栏底色和文字颜色

看图如下
VCRoot中设置self.navigationController.navigationBar.translucent = YES;
在这里插入图片描述

设置为self.navigationController.navigationBar.translucent = NO;
在这里插入图片描述

self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
在这里插入图片描述

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
在这里插入图片描述

第二示例:导航栏视图控制器管理视图控制器(页面跳转)

示例就是演示页面三连跳
默认情况下,非根视图控制器左上角有一个返回键并带名称,名称是上一个页面的title
涉及到添加自己的UIWindow的知识参考前面的UIWindow

先看图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

示例代码

先看一下工程目录结构
在这里插入图片描述

涉及到并做了修改的的文件写在下面,没用或者不做特殊处理的不贴出来了
AppDelegate.h
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (retain,nonatomic) UIWindow* window;

@end
AppDelegate.m
#import "AppDelegate.h"
#import "VCRoot.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    VCRoot* root = [VCRoot new];
    
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:root];
    
    self.window.rootViewController = nav;
    
    [self.window makeKeyAndVisible];
    return YES;
}


@end
VCRoot
#import "VCRoot.h"
#import "VCSecond.h"

@interface VCRoot ()

@end

@implementation VCRoot

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //添加一个红色view
    UIView* v = [UIView new];
    v.frame = CGRectMake(40, 40, 200, 200);
    v.backgroundColor = [UIColor redColor];
    [self.view addSubview:v];
    
    self.navigationItem.title = @"第一页";
    //导航栏透明度
    //YES:透明
    //NO:不透明
    self.navigationController.navigationBar.translucent = NO;
    
    //导航栏样式,分白底黑字,黑底白字两种
    //UIBarStyleDefault:白底黑字
    //UIBarStyleBlack:黑底白字
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    
    //导航栏右侧添加一个按钮,点击跳转到第二页
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithTitle:@"去第二页" style:UIBarButtonItemStyleDone target:self action:@selector(toSecond)];
    
    self.navigationItem.rightBarButtonItem = item;
    
    //视图控制器本身设置为蓝色
    self.view.backgroundColor = [UIColor blueColor];
}

-(void) toSecond{
    //跳转到第二个视图控制器
    VCSecond* second = [VCSecond new];
    //跳转方法
    //p1:视图控制器对象
    //p2:是否带动画
    [self.navigationController pushViewController:second animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
VCSecond.m
#import "VCSecond.h"
#import "VCThird.h"

@interface VCSecond ()

@end

@implementation VCSecond

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"第二页";
    
    UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"去第三页" style:UIBarButtonItemStyleDone target:self action:@selector(toThird)];
    
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    self.view.backgroundColor = [UIColor redColor];
}

-(void) toThird{
    //跳到第三个视图控制器
    VCThird* third = [VCThird new];
    
    [self.navigationController pushViewController:third animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
VCThird.m
#import "VCThird.h"

@interface VCThird ()

@end

@implementation VCThird

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"第三页";
    
    self.view.backgroundColor = [UIColor greenColor];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值