navigationTitle设置两按钮样式lable的代码

2016-12-15  本文已影响15人  BigBossZhu

效果图:

Snip20161215_1.png
#pragma mark - setuptitleView
- (void)setupNavButton {
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 152, 32)];
    for (int i = 0; i < self.navArr.count; ++i) {
        UILabel *lb = [UILabel new];
        lb.frame = CGRectMake(21+i*42, 0, 64, 32);
        lb.text = self.navArr[i];
        lb.font = [UIFont systemFontOfSize:18];
        lb.textColor = [UIColor colorWithHexString:@"9B9B9B"];
        lb.tag = i + 10;
        lb.textAlignment = NSTextAlignmentCenter;
        lb.userInteractionEnabled = YES;
        
        //创建手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(navTap:)];
        tap.delegate = self;
        [lb addGestureRecognizer:tap];
        
        UILabel *lb1 = [UILabel new];
        CGSize size = [self.navArr[i] sizeWithAttributes:@{ NSFontAttributeName: [UIFont fontWithName:nil size:18.0]}];
        lb1.frame = CGRectMake(lb.frame.origin.x + (64 - size.width)/2.0f , 32, size.width , 2);
        lb1.tag = 50 + i;
        //开始选择的lb
        if (i == _chooseTag) {
            //刚进来时也需要关联一下
            self.selectLable = lb;
            lb.textColor = [UIColor colorWithHexString:@"3BD395"];//浅绿色
            lb1.backgroundColor = [UIColor colorWithHexString:@"3BD395"];//浅绿色
        } else {
        }
        [titleView addSubview:lb];
        [titleView addSubview:lb1];
    }
    
    self.titleView = titleView;
    self.navigationItem.titleView = titleView;
}

#pragma mark - navTap
-(void)navTap:(UITapGestureRecognizer *)tap{
    
    NSInteger value = tap.view.tag - 10;
    
    for (NSInteger i = 0; i < self.navArr.count; i++) {
        
        if (i  == value ) {
            UILabel *lb = (UILabel *)[self.titleView viewWithTag:value + 10 ];
            UILabel *lb1 = (UILabel *)[self.titleView viewWithTag:value + 50];
            lb.font = [UIFont systemFontOfSize:18];
            [lb setTextColor:[UIColor colorWithHexString:@"3BD395"]];
            [lb1 setBackgroundColor:[UIColor colorWithHexString:@"3BD395"]];
            if ([lb.text isEqualToString:@"找人"]) {
//                NSLog(@"找人界面====%ld",(long)lb.tag);
            } else {
//                NSLog(@"找群界面%ld",(long)lb.tag);
            }
            //记录lable
            self.selectLable = lb;
            //手动调用一样的方法实现sugement的功能
             [self lableAction:lb];
            NSLog(@"selectedlb===%ld",self.selectLable.tag);
            continue;
        }else{
            UILabel *lb = (UILabel *)[self.titleView viewWithTag:i + 10 ];
            UILabel *lb1 = (UILabel *)[self.titleView viewWithTag:i + 50 ];
            [lb setTextColor:[UIColor colorWithHexString:@"9B9B9B"]];
            lb.font = [UIFont systemFontOfSize:18];
            [lb1 setBackgroundColor:[UIColor whiteColor]];
        }
    }
}

#pragma mark - lableAction的方法
- (void)lableAction:(UILabel *)sender {
//    [self.dataSource removeAllObjects];
//    [self.searchController.resultsSource removeAllObjects];
    if (sender.tag == 10) {
        DLog(@"全部");
    }
    else {
        DLog(@"未付款");
    }
    //    [_tableView reloadData];
}

#pragma mark - 初始化找人找群数据
- (NSArray *)navArr {
    if (!_navArr) {
        _navArr = [NSArray arrayWithObjects:@"找人",@"找群", nil];
    }
    return _navArr;
}

上一篇下一篇

猜你喜欢

热点阅读