<span style="font-size:14px;">//
// RootView.m
// UIlession6-segment
//
// Created by lanou3g on 15/8/30.
// Copyright (c) 2015年 lanou3g. All rights reserved.
//
#import "RootView.h"
@implementation RootView
-(void)dealloc{
[_segmente release];
[super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self addAllviews];
}
return self;
}
-(void)addAllviews{
//1.初始化一个segment
NSArray *array = @[@"1",@"2",@"3",@"4"];
_segmente = [[UISegmentedControl alloc]initWithItems:array];
_segmente.frame = CGRectMake(100, 60, 300, 100);
//2.设置segment的显示样式,segmentedControlStyle现在不让用了
// _segmente.segmentedControlStyle = UISegmentedControlStyleBezeled;
//3.设置segment的颜色
//@property(nonatomic,retain) UIColor *tintColor;
// _segmente.tintColor = [UIColor redColor];
//4.momentary 设置在点击后是否恢复到原样
// @property(nonatomic,getter=isMomentary) BOOL momentary;
// if set, then we don't keep showing selected state after tracking ends. default is NO
_segmente.momentary = YES;
_segmente.momentary = NO;
//5.numberOfSegments:只读,segment中的分段数目
// @property(nonatomic,readonly) NSUInteger numberOfSegments;
// _segmente.numberOfSegments = 5; //这种写法是错误的.其只有只读属性
//6.selectedSegmentIndex:用来设置选中项或者返回选中项
//@property(nonatomic) NSInteger selectedSegmentIndex;
//_segmente.selectedSegmentIndex:获取选中项
_segmente.selectedSegmentIndex = 2;//设置默认选择项索引
//7.设置指定索引的title:setTitle:(NSString *) forSegmentAtIndex:(NSUInteger)
[_segmente setTitle:@"red" forSegmentAtIndex:0 ];
//8.设定指定索引的图片:setImage:(UIImage *) forSegmentAtIndex:(NSUInteger)
// [_segmente setImage:[UIImage imageNamed:@"1.png"] forSegmentAtIndex:1];
//9.在指定索引插入一个选项并设置题目:insertSegmentWithTitle:(NSString *) atIndex:(NSUInteger) animated:(BOOL)
// [_segmente insertSegmentWithTitle:@"green" atIndex:1 animated:YES];
//10.在指定索引位置插入一个索引,并设置该索引的图片:insertSegmentWithImage:(UIImage *)image atIndex:(NSUInteger)segment animated:(BOOL)animated;
//在此填充图片需要设定图片的填充图片的填充格式:imageWithRenderingMode:(UIImageRenderingMode)renderingMode
[_segmente insertSegmentWithImage:[[UIImage imageNamed:@"2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] atIndex:1 animated:YES];
//11.移除指定的索引项:removeSegmentAtIndex:(NSUInteger) animated:(BOOL)
// [_segmente removeSegmentAtIndex:4 animated:YES];
//12.选项卡的宽度默认为0,此方法能够设置选项卡宽度:setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment
[_segmente setWidth:0 forSegmentAtIndex:0];
//13.设置选项卡内部文字或者图片与默认位置的偏移量,默认位置在选项卡的中心:setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment;其中CGSize是一个结构体.
CGSize size = CGSizeMake(5, 5);
[_segmente setContentOffset:size forSegmentAtIndex:0];
//14.设置指定索引项为不可选索引
[_segmente setEnabled:NO forSegmentAtIndex:0];
//15.UISegmentedControl继承于UIControl,所以UISegmentedControl也有事件的响应机制
//监听的是这个事件:UIControlEventValueChanged,值改变事件
[_segmente addTarget:self action:@selector(segmenteAction) forControlEvents:UIControlEventValueChanged];
//16.移除所有的segment:removeAllSegments
[_segmente removeAllSegments];
[self addSubview:_segmente];
}
-(void)segmenteAction{
NSLog(@"dfg");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
</span>
UISegmentedControl
最新推荐文章于 2020-07-08 06:09:52 发布