码迷,mamicode.com
首页 > 移动开发 > 详细

iOS-分段控制器-基本概念

时间:2016-06-18 01:15:07      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

 

可以直接复制使用

#import "FirstViewController.h"
#import "Masonry.h"

@interface FirstViewController ()

@property (nonatomic, strong) UISegmentedControl * segmentedControl_one;

@end

@implementation FirstViewController

#pragma mark - 生命周期
#pragma mark viewDidLoad
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self basicSetting];
    
    [self addSegmentedControl_one];
    
    [self insertOrDeleteInSegmendtedControl];
    
    [self settingSegmentProperty];
    
    [self obtainSectionItem];
}


#pragma mark - 系统代理

#pragma mark - 点击事件
- (void)segmentedControl_one:(UISegmentedControl *)sender
{
    NSLog(@"index: %ld",(long)sender.selectedSegmentIndex);
    
}

#pragma mark - 实现方法
#pragma mark 基本设置
- (void)basicSetting
{
    self.title = @"基本概念";
}

- (void)addSegmentedControl_one
{
    [self.view addSubview:self.segmentedControl_one];
    [self.segmentedControl_one mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.view).with.offset(10);
        make.right.mas_equalTo(self.view).with.offset(-10);
        make.top.mas_equalTo(self.view).with.offset(30);
        make.height.mas_equalTo(40);
    }];
}

- (void)insertOrDeleteInSegmendtedControl
{
    // 插入一段文字
    [self.segmentedControl_one insertSegmentWithTitle:@"插入的元素" atIndex:2 animated:YES];
    
    // 插入图片
    [self.segmentedControl_one insertSegmentWithImage:[[UIImage imageNamed:@"1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] atIndex:3 animated:YES];
    
    // 删除
    [self.segmentedControl_one removeSegmentAtIndex:0 animated:YES];
    
    // 根据下标修改分段标题
    [self.segmentedControl_one setTitle:@"修改的分段标题" forSegmentAtIndex:4];
}

- (void)settingSegmentProperty
{
    // 设置段落的宽度
    [self.segmentedControl_one setWidth:40.0f forSegmentAtIndex:2];
    
    // 设置分段中标题的位置 (0,0)是中心点的位置
    [self.segmentedControl_one setContentOffset:CGSizeMake(10, 10) forSegmentAtIndex:3];
}

- (void)obtainSectionItem
{
    //
    //获取指定索引选项的图片  imageForSegmentAtIndex:
    UIImageView *imageForSegmentAtIndex = [[UIImageView alloc]initWithImage:[self.segmentedControl_one imageForSegmentAtIndex:2]];
    imageForSegmentAtIndex.frame =CGRectMake(60.0,100.0,30.0,30.0);
    
    //获取指定索引选项的标题  titleForSegmentAtIndex
    UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(60.0,200.0,200.0,30.0)];
    titleForSegmentAtIndex.text = [self.segmentedControl_one titleForSegmentAtIndex:0];
    
    //获取总选项数   segmentedControl.numberOfSegments
    UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(60.0,300.0,30.0,200.0)];
    numberOfSegments.text = [NSString stringWithFormat:@"%lu",(unsigned long)self.segmentedControl_one.numberOfSegments];
    
    //获取指定索引选项的宽度  widthForSegmentAtIndex:
    UILabel *widthForSegmentAtIndex = [[UILabel alloc] initWithFrame:CGRectMake(40.0,400.0,200.0,30.0)];
    widthForSegmentAtIndex.text = [NSString stringWithFormat:@"宽度: %f",[self.segmentedControl_one widthForSegmentAtIndex:2]];
    
    //设置默认选择项索引
    self.segmentedControl_one.selectedSegmentIndex =2;
    // 设置色彩
    self.segmentedControl_one.tintColor = [UIColor redColor];
    
    self.segmentedControl_one.momentary = NO;//设置在点击后是否恢复原样
    
    [self.segmentedControl_one setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选
    BOOL enableFlag = [self.segmentedControl_one isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选
    NSLog(@"enableFlag: %d",enableFlag);
    
    [self.view addSubview:widthForSegmentAtIndex];
    [self.view addSubview:numberOfSegments];
    [self.view addSubview:titleForSegmentAtIndex];
    [self.view addSubview:imageForSegmentAtIndex];
    [self.view addSubview:self.segmentedControl_one];
}

#pragma mark - setter & getter

- (UISegmentedControl *)segmentedControl_one
{
    if (!_segmentedControl_one)
    {
        NSArray * array = @[@"第一段",@"第二段",@"第三段",@"第四段"];
        self.segmentedControl_one = [[UISegmentedControl alloc] initWithItems:array];
        
        [self.segmentedControl_one addTarget:self action:@selector(segmentedControl_one:) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentedControl_one;
}

@end

 

iOS-分段控制器-基本概念

标签:

原文地址:http://www.cnblogs.com/mancong/p/5595502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!