导航栏左侧或右侧添加多个item

导航栏左侧或右侧添加多个item_第1张图片

实现左侧或右侧两个item
以实现左侧为例子
一,系统方法实现
此种方法实现后两个按钮间隔很大

    UIBarButtonItem *leftBar=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navSearch"] style:UIBarButtonItemStylePlain target:self action:@selector(navClick1:)];
    UIBarButtonItem *leftBar2=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navEdit"] style:UIBarButtonItemStylePlain target:self action:@selector(navClick2:)];
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:leftBar,leftBar2,nil]];

二,我采用的自定义方式实现的

    //创建自定义视图
    UIView *leftBtnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
    leftBtnView.backgroundColor = [UIColor clearColor];
    //加载自定义视图
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtnView];

    //后退按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = H_FONT15;
    btn.frame = CGRectMake(0, 10, 20, 20);
    [btn setImage:[UIImage imageNamed:@"returnImage"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(navClick1:) forControlEvents:UIControlEventTouchUpInside];
    [leftBtnView addSubview:btn];

    //关闭按钮
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn2.titleLabel.font = H_FONT15;
    btn2.frame = CGRectMake(30, 10, 20, 20);
    [btn2 setImage:[UIImage imageNamed:@"closeImage"] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(navClick2:) forControlEvents:UIControlEventTouchUpInside];
    [leftBtnView addSubview:btn2];

你可能感兴趣的:(oc基础)