UINavigationController之NavigationItem

本文详细介绍如何使用NavigationItem组件创建各种样式UIBarButtonItem,包括左、右BarButtonItem的创建与设置,以及导航条标题和返回按钮的定制方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NavigationItem包括LeftBarButtonItem、RightBarButtonItem、BackBarButtonItem以及TitleView四个部分

  • 创建左右 UIBarButtonItem
创建系统样式的 UIBarButtonItem

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:nil];

创建文字样式的 UIBarButtonItem

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"短信" style:UIBarButtonItemStyleDone target:self action:nil];

创建图片样式的 UIBarButtonItem

UIImage *image = [[UIImage imageNamed:@"iconfont-duanxin"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];  // 32x32 显示原色

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:nil];

创建继承自UIView样式的 UIBarButtonItem

UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];

UIBarButtonItem *item  = [[UIBarButtonItem alloc] initWithCustomView:button];
  • 添加左右 UIBarButtonItem
[self.navigationItem setLeftBarButtonItem:leftItem];
[self.navigationItem setLeftBarButtonItems:@[leftItem1, leftItem2, leftItem3]];

[self.navigationItem setRightBarButtonItem:rightItem];
[self.navigationItem setRightBarButtonItems:@[rightItem1, rightItem2, rightItem3]];
  • 设置导航条返回按钮 BackBarButtonItem
[self.navigationItem setBackBarButtonItem:backItem]; // 下一级界面生效,无需设置它的交互事件
  • 设置 BarButtonItem 的一些属性
[item setTintColor:[UIColor redColor]];  // 设置item文字或图片的镂空颜色
[item setEnabled:YES];                   // 设置item的交互事件是否可用
  • 使用NavigationItem设置导航条标题
self.title = @"导航条标题";

self.navigationItem.title = @"导航条标题";

UIView *titleView = [[UIView alloc] init];
[titleView setFrame:CGRectMake(0, 0, 100, 44)];
[titleView setBackgroundColor:[UIColor orangeColor]];
[self.navigationItem setTitleView:titleView];
  • 其他
设置备注,关于这个界面的说明,工作很少用的
self.navigationItem.prompt = @"我是备注";
  • 去除导航条返回按钮的标题(将标题向上移动60)
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
  • 去除导航条顶部的分割线
if ([_tabBarCtrl.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
        NSArray *list=_tabBarCtrl.navigationController.navigationBar.subviews;
        for (id obj in list) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView=(UIImageView *)obj;
                NSArray *list2=imageView.subviews;
                for (id obj2 in list2) {
                    if ([obj2 isKindOfClass:[UIImageView class]]) {
                        UIImageView *imageView2=(UIImageView *)obj2;
                        imageView2.hidden=YES;
                    }
                }
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值