自定義沒(méi)有圓角的類(lèi)似UISegmentedControl控件

IMG_2255.jpg
//
//  HeadView.h
//  自定義Seg控件
//
//  Created by Vanessa on 16/5/31.
//  Copyright ? 2016年 Vanessa. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HeadView : UIView

@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSMutableArray <UIButton *>*buttonArray;
@property (nonatomic, strong) UIColor *tint_Color;
@property (nonatomic, strong) UIFont *textFont;
@property (nonatomic, assign) NSInteger selectedIndex;
@property (nonatomic, strong) UIButton *selectedButton;
@property (nonatomic, strong) id delegate;
@property (nonatomic) SEL action;


-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)event;

@end
//
//  HeadView.m
//  自定義Seg控件
//
//  Created by Vanessa on 16/5/31.
//  Copyright ? 2016年 Vanessa. All rights reserved.
//

#import "HeadView.h"

@implementation HeadView

-(instancetype)init {
    
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        _textFont = [UIFont systemFontOfSize:13];
        _tint_Color = [UIColor blackColor];
        _selectedIndex = 0;
        _buttonArray = [NSMutableArray array];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame {
    //初始化默認(rèn)數(shù)據(jù)
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        _textFont = [UIFont systemFontOfSize:13];
        _tint_Color = [UIColor blackColor];
        _selectedIndex = 0;
        _buttonArray = [NSMutableArray array];
    }
    return self;
}

static int tagIndex = 151515;
-(void)setTitleArray:(NSArray *)titleArray {
    
    
    _titleArray = titleArray;
    CGFloat width = self.frame.size.width / titleArray.count;
    for (int i = 0; i < _titleArray.count; i++) {
        NSString *string = _titleArray[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(width * i, 0, width, self.frame.size.height);
        button.tag = tagIndex + i;
        button.titleLabel.font = self.textFont;
        [button setTitle:string forState:UIControlStateNormal];
        [button setTitle:string forState:UIControlStateSelected];
        [button setTitleColor:self.tint_Color forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor]forState:UIControlStateSelected];
        [button addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.borderWidth = 0.5;
        button.layer.borderColor = _tint_Color.CGColor;
        if (i == self.selectedIndex) {
            button.selected = YES;
            self.selectedButton = button;
            [button setBackgroundColor:self.tint_Color];
        }
        [self.buttonArray addObject:button];
        [self addSubview:button];
    }
}

-(void)setTint_Color:(UIColor *)tint_Color {
    
    _tint_Color = tint_Color;
    for (UIButton *button in self.subviews) {
        if ([button isKindOfClass:[UIButton class]]) {
            [button setTitleColor:tint_Color forState:UIControlStateNormal];
            button.layer.borderColor = tint_Color.CGColor;
        }
        if (button.selected == YES) {
            [button setBackgroundColor:tint_Color];
        }
    }
}

-(void)setTextFont:(UIFont *)textFont {
    
    _textFont = textFont;
    for (UIButton *button in self.subviews) {
        if ([button isKindOfClass:[UIButton class]]) {
            button.titleLabel.font = textFont;
        }
    }
}

-(void)setSelectedIndex:(NSInteger)selectedIndex {
    
    _selectedIndex = selectedIndex;
    [self didClickButton:self.buttonArray[selectedIndex]];
}

-(void)didClickButton:(UIButton *)button {
    
    _selectedIndex = button.tag - tagIndex;
    [self.selectedButton setBackgroundColor:[UIColor whiteColor]];
    self.selectedButton.selected = NO;
    self.selectedButton = button;
    self.selectedButton.selected = YES;
    [self.selectedButton setBackgroundColor:self.tint_Color];
    if (!self.delegate || !_action) {
        return;
    }
    [self.delegate performSelector:_action withObject:self];
}

-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)event{
    
    if (event == UIControlEventValueChanged) {
        self.delegate = target;
        self.action = action;
    }
}
@end

具體使用步驟:

//添加控件
HeadView *headView = [[HeadView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 44)];
    headView.titleArray = @[@"未完成",@"已完成"];
    headView.selectedIndex = self.currentIndex;//設(shè)置默認(rèn)選擇項(xiàng)索引
    headView.tint_Color = [UIColor lightGrayColor];
    [headView addTarget:self action:@selector(didclickHeadview:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:headView];

#pragma mark - Event
-(void)didclickHeadview:(HeadView *)headview {
 
    NSLog(@"%s---%zd",__func__,headview.selectedIndex);
    
}

http://pan.baidu.com/s/1hrN6USs

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末许溅,一起剝皮案震驚了整個(gè)濱河市跃洛,隨后出現(xiàn)的幾起案子拙吉,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡疫衩,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén)荣德,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)闷煤,“玉大人,你說(shuō)我怎么就攤上這事涮瞻±鹉茫” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵署咽,是天一觀的道長(zhǎng)近顷。 經(jīng)常有香客問(wèn)我,道長(zhǎng)宁否,這世上最難降的妖魔是什么窒升? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮慕匠,結(jié)果婚禮上饱须,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布嫁怀。 她就那樣靜靜地躺著,像睡著了一般酪呻。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上狠角,一...
    開(kāi)封第一講書(shū)人閱讀 51,301評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音蚪腋,去河邊找鬼丰歌。 笑死,一個(gè)胖子當(dāng)著我的面吹牛屉凯,可吹牛的內(nèi)容都是我干的立帖。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼悠砚,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼晓勇!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤绑咱,失蹤者是張志新(化名)和其女友劉穎绰筛,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體描融,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡铝噩,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了窿克。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片骏庸。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖年叮,靈堂內(nèi)的尸體忽然破棺而出具被,到底是詐尸還是另有隱情,我是刑警寧澤只损,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布一姿,位于F島的核電站,受9級(jí)特大地震影響改执,放射性物質(zhì)發(fā)生泄漏啸蜜。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一辈挂、第九天 我趴在偏房一處隱蔽的房頂上張望衬横。 院中可真熱鬧,春花似錦终蒂、人聲如沸蜂林。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)噪叙。三九已至,卻和暖如春霉翔,著一層夾襖步出監(jiān)牢的瞬間睁蕾,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工债朵, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留子眶,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓序芦,卻偏偏與公主長(zhǎng)得像臭杰,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子谚中,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容