navigationTitle設(shè)置兩按鈕樣式lable的代碼

效果圖:

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;
        
        //創(chuàng)建手勢
        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) {
            //剛進(jìn)來時也需要關(guān)聯(lián)一下
            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;
            //手動調(diào)用一樣的方法實(shí)現(xiàn)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 - 初始化找人找群數(shù)據(jù)
- (NSArray *)navArr {
    if (!_navArr) {
        _navArr = [NSArray arrayWithObjects:@"找人",@"找群", nil];
    }
    return _navArr;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容