您的位置:首页 > 产品设计 > UI/UE

定义UISegmetteController

2016-08-09 15:09 78 查看
.m文件

#import "YSegmentController.h"

@implementation YSegmentController

-(id)init

{

    self = [super init];

    if (self) {

        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width/3, 35);

        

    }

    

    return self;

}

-(id)initWithFrame:(CGRect)frame

{

    

    self = [super initWithFrame:frame];

    

    if (self) {

        

        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width/3, 35);

        UIView *bgLingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];

        

        bgLingView.backgroundColor = [UIColor clearColor];

        

        bgLingView.layer.cornerRadius = 20;

        bgLingView.layer.masksToBounds = YES;

        bgLingView.layer.borderColor = [UIColor whiteColor].CGColor;

        bgLingView.layer.borderWidth = 1.0;

        

        [self addSubview:bgLingView];

        

        NSArray *arr = @[@"qqq",@"www"];

        

        UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:arr];

        

        segment.frame = CGRectMake(2, 2, bgLingView.bounds.size.width-4, bgLingView.bounds.size.height-4);

        segment.layer.cornerRadius = 18;

        segment.layer.masksToBounds = YES;

        segment.selectedSegmentIndex = 0;

        

        

        segment.tintColor = [UIColor clearColor];//去掉颜色,现在整个segment都看不见

        NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],

                                                 NSForegroundColorAttributeName: [UIColor orangeColor]};

        [segment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性

        NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],

                                                   NSForegroundColorAttributeName: [UIColor whiteColor]};

        [segment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];

       

        [segment setBackgroundImage:[self imageWithColor:[UIColor orangeColor] size:CGSizeMake(20, 20)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

        [segment setBackgroundImage:[self imageWithColor:[UIColor whiteColor] size:CGSizeMake(20, 20)] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

        segment.apportionsSegmentWidthsByContent = NO;

        

        [segment addTarget:self action:@selector(segmentControlPlay:) forControlEvents:UIControlEventValueChanged];

        

        

        [self addSubview:segment];

        

        

        

    }

    

    

    return self;

    

}

-(void)segmentControlPlay:(UISegmentedControl*)segment

{

    

    NSLog(@"%ld",segment.selectedSegmentIndex);

    if ([_delegate respondsToSelector:@selector(ysegment:withView:withIndex:)]) {

        [_delegate ysegment:segment withView:self withIndex:segment.selectedSegmentIndex];

    }

    

}

-(UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size

{

    CGRect rect = CGRectMake(0, 0, size.width, size.height);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context,color.CGColor);

    CGContextFillRect(context, rect);

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    return img;

}

.h文件

#import <UIKit/UIKit.h>

@protocol YSegmentControllerDelegate <NSObject>

-(void)ysegment:(UISegmentedControl*)segment withView:(UIView*)view withIndex:(NSInteger)index;

@end

@interface YSegmentController : UIView

@property(nonatomic,assign) id<YSegmentControllerDelegate>delegate;

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: