UINavigationController常用属性设置

// 1 设置导航栏标题
// 方法1
self.title = @"导航栏控制器";
// 方法2 当有tabbarController时使用该方法,避免影响标题设置
self.navigationItem.title = @"导航栏控制器标题";


// 2 设置自定义导航栏标题子视图
UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];
titleImageView.image = [UIImage imageNamed:@"image"];
self.navigationItem.titleView = titleImageView;


// 3 设置导航栏控制器-右按钮
// 3-1 设置单个按钮
// self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStyleDone target:self action:@selector(nextClick:)];
// 3-2 设置多个按钮
UIBarButtonItem *playItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(nextClick:)];
UIBarButtonItem *stopItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(doneClick:)];
NSArray *itemsRight = [NSArray arrayWithObjects:playItem, stopItem, nil];
self.navigationItem.rightBarButtonItems = itemsRight;

- (void)nextClick:(UIBarButtonItem *)barButton
{
    NSLog(@"你点击了 next ");
}

- (void)doneClick:(UIBarButtonItem *)barButton
{
    NSLog(@"你点击了 done ");
}

// 4 设置导航栏控制器-左按钮
// 4-1 设置单个按钮
// self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(sureClick:)];
// 3-2 设置多个按钮
UIBarButtonItem *sureItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(sureClick:)];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancelClick:)];
NSArray *itemsLeft = [NSArray arrayWithObjects:sureItem, cancelItem, nil];
self.navigationItem.leftBarButtonItems = itemsLeft;

- (void)sureClick:(UIBarButtonItem *)barButton
{
    NSLog(@"你点击了 sure ");
}

- (void)cancelClick:(UIBarButtonItem *)barButton
{
    NSLog(@"你点击了 cancel ");
}


// 5 设置导航栏控制器显示样式
// 样式
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
// 导航按钮颜色
self.navigationController.navigationBar.tintColor = [UIColor greenColor];
// 背景颜色
self.navigationController.navigationBar.barTintColor = [UIColor yellowColor];
// 背景图片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"image"] forBarMetrics:UIBarMetricsDefault];
// 背景阴影
self.navigationController.navigationBar.shadowImage = [UIImage imageNamed:@"image"];
// 字体标题设置(字体大小、字体颜色设置)
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0],  NSForegroundColorAttributeName:[UIColor yellowColor]}];

// 6 导航栏控制器的显示,或隐藏
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 80.0, (CGRectGetWidth(self.view.bounds) - 10.0 * 2), 30.0)];
[self.view addSubview:button];
button.backgroundColor = [UIColor redColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
[button setTitle:@"隐藏导航栏" forState:UIControlStateNormal];
[button setTitle:@"显示导航栏" forState:UIControlStateSelected];
[button addTarget:self action:@selector(hiddenClick:) forControlEvents:UIControlEventTouchUpInside];

- (void)hiddenClick:(UIButton *)button
{
    button.selected = !button.selected;
    
    if (button.selected)
    {
//        self.navigationController.navigationBarHidden = YES; // 方法1
        [self.navigationController setNavigationBarHidden:YES animated:YES]; // 方法2 常用方法,特别是视图控制器之间跳转时避免产生黑色区域
    }
    else
    {
//        self.navigationController.navigationBarHidden = NO;
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值