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];
       });
    }];
相关推荐
facaixxx202428 分钟前
OpenClaw在Mac电脑上部署安装教程:两种方法安装龙虾方法
macos
ITKEY_32 分钟前
macOS安装fvm管理flutter版本
flutter·macos
刘一说2 小时前
告别“版本漂移”:彻底解决 macOS 上 NVM 默认 Node 版本失效的难题
macos·node·nvm
yijianxiangde1004 小时前
iOS 无根越狱前期准备
ios
库奇噜啦呼4 小时前
【iOS】Effective Objective-C第一章
开发语言·ios·objective-c
知智前沿4 小时前
OpenClaw 本地部署详细教程(Windows+Mac 双系统)
windows·macos
想要AC的sjh5 小时前
【硬核实战】Mac mini M4 部署 OpenClaw + Ollama 本地大模型:从零到一打通飞书机器人
macos·机器人·飞书
John Song5 小时前
Openclaw的安装(mac版)
macos·openclaw
Jamesvalley6 小时前
【openclaw】mac安装
macos·openclaw
2501_916008897 小时前
iPhone 上怎么抓 App 的网络请求,在 iOS 设备上捕获网络请求
android·网络·ios·小程序·uni-app·iphone·webview