iOS 导航栏view创建

一. 思路
  • 给ViewController创建一个分类,里面放导航栏view的创建方法
  • 如果需要使用button,button的点击使用block来解决
二. 代码
1. 首先需要一个可以使用block处理点击事件的button,参考这里
2. 然后给ViewController创建一个分类,并在里面实现代码
复制代码
- (UIView *)createNavigationItemTitleViewWithTitle:(NSString *)title{
    UILabel *label = [[UILabel alloc] init];
    label.text = title;
    label.font = ISFont_17;
    label.textColor = [UIColor whiteColor];
    CGSize labelSize = [label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:17], NSFontAttributeName, nil]];
    label.frame = CGRectMake(0, 0, labelSize.width, labelSize.height);
    return label;
}

- (UIBarButtonItem *)createLeftBarButtonItemWithImage:(UIImage *)leftBarImage touch:(void(^)(UIButton *button))touchBlock{
    BlockButton * leftButton = [BlockButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 20, 20);
    [leftButton setImage:leftBarImage forState:UIControlStateNormal];
    
    [leftButton addTapBlock:^(UIButton *button) {
        touchBlock(button);
    }];
    
    return [[UIBarButtonItem alloc] initWithCustomView:leftButton];
}
3. 在需要的位置调用即可
复制代码
//设置vc标题
self.navigationItem.titleView = [self createNavigationItemTitleViewWithTitle:self.labelInfo.szLabelName];
//设置返回按钮
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    __weak typeof(self) weakSelf = self;
    self.navigationItem.leftBarButtonItem = [self createLeftBarButtonItemWithImage:[UIImage imageNamed:@"backbutton"] touch:^(UIButton *button) {
       dispatch_async(dispatch_get_main_queue(), ^{
           [weakSelf.navigationController popViewControllerAnimated:YES];
       });
    }];
相关推荐
sunfdf8 小时前
移动硬盘上的文件消失了?以下是Mac电脑解决方法
macos·电脑
wonderomg17 小时前
Mac安装openclaw步骤
macos·openclaw
humors22118 小时前
[原创]AI工具:读取手机系统文件工具
windows·ios·安卓·鸿蒙·文件·苹果·读取
whatzhang00718 小时前
在 macOS 上从零配置 Vim:开启语法高亮 + 安装 vim-polyglot + 设置 gruvbox 主题
macos·vim·excel
humors22118 小时前
[原创]AI工具:手机文件查杀病毒工具
windows·ios·手机·安卓·鸿蒙·杀毒·苹果
徐健峰1 天前
Claude Code 安装完全指南(Mac 版):Git、环境变量、PATH 与常见报错一次讲清(2026)
git·macos·arcgis
AppOS1 天前
手把手教你 Openclaw 在 Mac 上本地化部署,保姆级教程!接入飞书打造私人 AI 助手
人工智能·macos·飞书
2501_915918411 天前
iOS性能测试工具 Instruments、Keymob的使用方法 不局限 FPS
android·ios·小程序·https·uni-app·iphone·webview
qing222222221 天前
Linux中修改mac地址(重启后依然生效)
linux·服务器·macos
GameTomato1 天前
【Xcode】【资源创建失败】【XCode更新最新版】XCode更新最新版,创建资源文件夹一直读取不到的问题
ide·macos·xcode