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];
       });
    }];
相关推荐
不惑_19 分钟前
[特殊字符] 在 macOS 上设置 SQLite
数据库·macos·sqlite
2501_9275410926 分钟前
Mac多功能音视频AI处理工具VideoProc Converter AI
macos·音视频
denggun1234538 分钟前
ios-AVIF
macos·ios·cocoa
ajassi20001 小时前
开源 Objective-C IOS 应用开发(六)Objective-C 和 C语言
ios·开源·objective-c
Digitally17 小时前
如何在不使用iTunes的情况下在电脑上访问iPhone文件
ios·电脑·iphone
QuantumLeap丶17 小时前
《Flutter全栈开发实战指南:从零到高级》- 14 -网络请求与数据解析
flutter·ios·dart
RollingPin17 小时前
iOS 内存管理之 autoreleasePool
ios·内存管理·runtime·autoreleasepool
BlackWolfSky19 小时前
鸿蒙文件操作
macos·华为·harmonyos·鸿蒙
2501_9159214320 小时前
查看iOS App实时日志的正确方式,多工具协同打造高效调试与问题定位体系(2025最新指南)
android·ios·小程序·https·uni-app·iphone·webview
私人珍藏库20 小时前
[Android] AR绘画素描1.0版(AR Draw - Sketch Anime Cartoon 1.0)
macos·ar·sketch