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];
       });
    }];
相关推荐
茶底世界之下1 天前
蓝调鱼市的一盏暖灯:用 Awaking 把冷海与灯光分开
ios·app·产品
独隅1 天前
WebStorm macOS 安装部署与高效调优完全指南
ide·macos·webstorm
独隅1 天前
WebStorm macOS 安装调优与实战效能全景
ide·macos·webstorm
2601_967987372 天前
安装 Redis 或 Redis Stack
linux·windows·redis·macos·安装
高调的涵涵2 天前
1GB 文件,df 说没占地方
macos
茶底世界之下2 天前
窗边白雏菊与旧书:用 Awaking 留住一束暖光
ios·app·产品
奶油话梅糖2 天前
苹果的内核跑上了安卓手机:2,631 Star 的 HakiOS 让 XNU 在 Adreno GPU 上画出 118 帧
android·macos·智能手机
独隅3 天前
WebStorm macOS 安装调优与实战效果全景
ide·macos·webstorm
茶底世界之下3 天前
阴天海边的蓝船:用 Awaking 把灰蓝调成清亮主角
ios·app·产品
木子欢儿3 天前
国内 macOS/Linux 安装 rust
linux·运维·服务器·macos·rust