定义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
#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
相关文章推荐
- 从一个工作流流转中人员设定的例子看WBISF流程引擎所定义的用户授权安全模型
- DataGrid for jQuery 定义表头
- “菜单”“定义”“嵌套”列表
- 在android.app.Application中定义全局变量
- 表的定义
- 江苏电信欢go重新定义宽带标准
- 结构体定义:struct与typedef struct 用法详解和用法小结
- epoch的定义 和 iteration的定义
- flow.js/typescript 这类定义参数类型的意义何在
- 从digg在新媒体中的角色谈如何定义新媒体
- ADO.NET Entity Framework如何:定义具有修改存储过程的模型(实体框架)
- linux alias命令参数及用法详解--linux定义命令别名alias
- C语言中定义与声明的本质区别:有无内存分配
- Nginx中定义404页面并且返回404状态码的正确方法
- iOS 重新定义cell的分割线样式
- php中定义网站根目录的常用方法
- css IE6和W3C盒子定义区别
- Ubuntu查找文件夹中内容包含关键字的文件(OpenCV中查找对应函数定义的技巧)
- Google Gadgets (4):定义 Gadget 属性
- ADO.NET Entity Framework 如何:定义单个实体映射到两个表的模型