定义UISegmetteController
来源:互联网 发布:unity3d模型 编辑:程序博客网 时间:2024/06/05 23:05
.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;
}
#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
- 定义UISegmetteController
- 定义
- 定义
- 定义
- <%! 定义 %>与 <% 定义 %>区别
- <%! 定义 %>与 <% 定义 %>区别
- <%! 定义 %>与 <% 定义 %>区别
- HTML 元素定义,属性定义,实体定义
- 接口定义
- 定义初始化
- 内容管理定义
- 智能手机定义
- XMLHTTP定义
- ABI 定义
- “卐”定义
- META定义
- 组件定义
- POSITION定义
- java中的byte[]和其他数据类型转换问题
- 今日头条加载进度条
- Mac下清除DNS
- split、subString和indexOf的用法
- 获取数字签名(.keystore)SHA1、MD5值
- 定义UISegmetteController
- sql server表空间如何回收
- oracle实现某一列的值转换为列标题
- 求二叉树的深度
- poj 1611 The Suspects
- 远程推送原理
- java:Class.forName()机制详解
- 一种两位小数点数字输入的控制方法
- MATLAB学习笔记