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];
       });
    }];
相关推荐
m_136871 小时前
Mac Intel 芯片部署 YOLO(Docker 方式,支持离线打包与 Compose 管理)
yolo·macos·docker
伐尘4 小时前
【mac】macOS上的实用Log用法
macos
liulilittle8 小时前
UNIX/macOS路由表查询原理与实现
服务器·开发语言·c++·macos·unix·编程语言
MaoJiu8 小时前
Flutter混合开发:在iOS工程中嵌入Flutter Module
flutter·ios
xingxing_F8 小时前
PastePal for Mac 剪贴板历史记录管理器
macos
snows_l8 小时前
如何在MacOS上卸载并且重新安装Homebrew
macos·homebrew·utils
2501_915921439 小时前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
m_1368710 小时前
YOLOv8 在 Intel Mac 上的 Anaconda 一键安装教程
yolo·macos
m_1368710 小时前
Mac Intel 芯片 Docker 一键部署 Neo4j 最新版本教程
macos·docker·neo4j
我发在否10 小时前
Lua > Mac Mini M4安装openresty
macos·lua·openresty