iOS top image text button (the bottom horizontal line is selected) Toggle

//

//  TeamHeaderView.h

// HengTaiXinGolf

//

// Created by Ouyang Rong on 16/4/16.

//  Copyright © 2016 HengTaiXinGolf. All rights reserved.

//


#import <UIKit/UIKit.h>



typedef void (^teamViewBlock)(id);



@interface TeamHeaderView : UIView



@property (nonatomic,copy) teamViewBlock teamBlock;













@end

//

//  TeamHeaderView.m

// HengTaiXinGolf

//

// Created by Ouyang Rong on 16/4/16.

//  Copyright © 2016 HengTaiXinGolf. All rights reserved.

//


#import "TeamHeaderView.h"


@implementation TeamHeaderView




-(instancetype)initWithFrame:(CGRect)frame{


    self = [super initWithFrame:frame];

    

    if (self) {

        

        self.backgroundColor = kColor(231, 231, 231);

        

        [self createUI];

        

    }


    return self;


}


-(void)createUI{



    NSArray * arr = @[@"我的球队",@"全部球队"];

    NSArray * imgArr = @[@"wdqd_h",@"rmqd_h"];

    NSArray * imgSelArr = @[@"wdqd_l",@"rmqd_l"];

    for (NSInteger i = 0; i < [arr count]; i ++) {

        

        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.frame = CGRectMake(i * IPHONE_WIDTH/2 + 80 * KSCALE_X, 0, 170 * KSCALE_X, 75 * KSCALE_X);

        btn.tag = 800 + i;

        [btn setTitle:arr[i] forState:UIControlStateNormal];

        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [btn setTitleColor:[UIColor colorWithRed:0/255.0 green:159/255.0 blue:251/255.0 alpha:1.0] forState:UIControlStateSelected];

        [btn setImage:[UIImage imageNamed:imgArr[i]] forState:UIControlStateNormal];

        [btn setImage:[UIImage imageNamed:imgSelArr[i]] forState:UIControlStateSelected];

        

        [btn setBackgroundImage:[UIImage imageNamed:@"dh1"] forState:UIControlStateSelected];

        

        btn.titleLabel.font = [UIFont systemFontOfSize:13 * KWIDTH];

        

        [btn addTarget:self action:@selector(tipBtnAction:) forControlEvents:UIControlEventTouchUpInside];

        

        

        //   调整图片和文字间距

        

        

        [btn setTitleEdgeInsets:UIEdgeInsetsMake(25 * KSCALE_X, - 30 * KSCALE_X, 16 * KSCALE_X, 8)];

        

        [btn setImageEdgeInsets:UIEdgeInsetsMake(16 * KSCALE_X, 0 , 16 * KSCALE_X, 120 * KSCALE_X)];

        

        

        

        [self addSubview:btn];

        UIImageView * line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 75 * KSCALE_X - 1.5, IPHONE_WIDTH, 1)];

        line.backgroundColor = kColor(96, 145, 214);

        [self addSubview:line];

        

        

    }

    

    

    


}




-(void)tipBtnAction:(id)sender{


    if (self.teamBlock) {

        

        self.teamBlock(sender);

        

    }

    


}















/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/


@end



-(void)createHeaderView{


    _teamHeaderView = [[TeamHeaderView alloc]initWithFrame:CGRectMake(0, 0,IPHONE_WIDTH, 75 * KSCALE_X)];

   

    [self.view addSubview:_teamHeaderView];


    __weak TeamViewController *VC = self;

    

    _teamHeaderView.teamBlock = ^(UIButton *sender){

    

    

        [VC teamTopBtnAction:sender];

    

    };

    

    _oldBtn = (UIButton *)[VC.view viewWithTag:800];

    _oldBtn.selected = YES;

    

    


}


-(void)teamTopBtnAction:(UIButton *)sender{


    _oldBtn.selected = NO;

    

    sender.selectedYES;

    

    _oldBtn = sender;

    

    switch (sender.tag) {

        case 800:

        {

            DSLog(@"我的球队");

        }

            break;

        case 801:

        {

            DSLog(@"全部球队");

 

        }

            break;

        default:

            break;

    }

    


}



Guess you like

Origin blog.csdn.net/qq_27247497/article/details/51168432