UIKit 框架之UISegmentedControl

//

//  ViewController.m

//  UISegmentedControl

//

//  Created by City--Online on 15/5/20.

//  Copyright (c) 2015年 XQB. All rights reserved.

//



#import "ViewController.h"



@interface ViewController ()

@property(nonatomic,strong) UISegmentedControl *segmentedControl;

@end



@implementation ViewController



- (void)viewDidLoad {

    [super viewDidLoad];

    _segmentedControl =[[UISegmentedControl alloc]initWithFrame:CGRectMake(10, 100, 200, 50)];

    //属性无效

//    _segmentedControl.segmentedControlStyle=UISegmentedControlStyleBar;

           

    //点击后是否恢复原样

    _segmentedControl.momentary=NO;

    //是否根据segment的内容改变segment的宽度

    _segmentedControl.apportionsSegmentWidthsByContent=YES;

    

    //插入

    [_segmentedControl insertSegmentWithTitle:@"0" atIndex:0 animated:YES];

    [_segmentedControl insertSegmentWithTitle:@"1" atIndex:1 animated:YES];

    [_segmentedControl insertSegmentWithTitle:@"2" atIndex:2 animated:YES];

    //添加时若插入索引已经存在,则已最新的索引为准重新排序 例如下面的

    [_segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"1.jpg"] atIndex:0 animated:YES];

    [_segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"2.jpg"] atIndex:1 animated:YES];

    //删除时也是和插入一样 删除一个之后重新排序

    [_segmentedControl removeSegmentAtIndex:0 animated:YES];

    [_segmentedControl removeSegmentAtIndex:0 animated:YES];

    //删除所有的块

//    [_segmentedControl removeAllSegments];

    

    //标题

    [_segmentedControl setTitle:@"001" forSegmentAtIndex:0];

    NSString *titleIndex2=[_segmentedControl titleForSegmentAtIndex:2];

    NSLog(@"title=%@",titleIndex2);

    

    //图片

    [_segmentedControl setImage:[UIImage imageNamed:@"1.jpg"] forSegmentAtIndex:1];

    UIImage *imgIndex1=[_segmentedControl imageForSegmentAtIndex:1];

    NSLog(@"UIImage=%@",imgIndex1);

    

    //宽度

    [_segmentedControl setWidth:30 forSegmentAtIndex:2];

    //如果未设置则获取的为0

    float widthIndex1=[_segmentedControl widthForSegmentAtIndex:2];

    NSLog(@"width=%lf",widthIndex1);

    

    //内容偏移量

    [_segmentedControl setContentOffset:CGSizeMake(5, 5) forSegmentAtIndex:0];

    CGSize size=[_segmentedControl contentOffsetForSegmentAtIndex:2];

    NSLog(@"contentOffset=%@",NSStringFromCGSize(size));

    

    //可用性

    [_segmentedControl setEnabled:NO forSegmentAtIndex:0];

    BOOL enable=[_segmentedControl isEnabledForSegmentAtIndex:0];

    NSLog(@"%d",enable);

    

    //选中的索引值

    _segmentedControl.selectedSegmentIndex=2;

    

    //设置segmented的颜色

    _segmentedControl.tintColor=[UIColor redColor];



    //设置文本样式

    NSDictionary *dic=@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSStrikethroughStyleAttributeName:@1};

    [_segmentedControl setTitleTextAttributes:dic forState:UIControlStateNormal];

    

    //获取分段控件的个数

   NSInteger count= [_segmentedControl numberOfSegments];

    NSLog(@"numberOfSegments=%ld",count);

    

    [_segmentedControl addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventValueChanged];



    [self.view addSubview:_segmentedControl];

    

}



-(void)valueChanged

{

    NSLog(@"selectedIndex=%ld",_segmentedControl.selectedSegmentIndex);

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end

 UIKit 框架之UISegmentedControlUIKit 框架之UISegmentedControl

你可能感兴趣的:(UIKit 框架之UISegmentedControl)