iOS-视图-UISegmentedControl 分栏控件 分段空间 选择条

高度不可变

UIImage* image = [UIImage imageNamed:@"1"];
    //初始化 item可以是图片或文字,系统自动控制每个item的大小
    UISegmentedControl* sc = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"视频", image, @"图片", nil]];
    sc.frame = CGRectMake(10, 20, 300, 50);
    
    //设置类型
    //[sc setSegmentedControlStyle:UISegmentedControlStyleBar];
    //点击弹起后时候恢复原样
    [sc setMomentary:YES];
    //有多少段
    //[sc numberOfSegments];
    NSLog(@"sc--numberOfSegments= %lu",(unsigned long)sc.numberOfSegments);
    //自动调整段的宽度
    //[sc setApportionsSegmentWidthsByContent:YES];
    
    //默认选中
    sc.selectedSegmentIndex = 1;
    [sc setSelectedSegmentIndex:1];
    //设置颜色
    sc.tintColor = [UIColor blueColor];
    [sc setTintColor:[UIColor blueColor]];
    
    //更改段中的内容
    [sc setImage:[UIImage imageNamed:@"2"] forSegmentAtIndex:1];
    [sc setTitle:@"音乐" forSegmentAtIndex:0];
    //取值
    //[sc titleForSegmentAtIndex:0];
    //[sc imageForSegmentAtIndex:1];
    //0.0 为自动调整
    [sc setWidth:20.0 forSegmentAtIndex:1];
    [sc widthForSegmentAtIndex:1];
    
    [sc setContentOffset:CGSizeMake(10, 10) forSegmentAtIndex:1];
    [sc contentOffsetForSegmentAtIndex:1];
    
    //动态添加
    [sc insertSegmentWithImage:[UIImage imageNamed:@"3"] atIndex:2 animated:YES];
    [sc insertSegmentWithTitle:@"=" atIndex:3 animated:YES];
    //删除某一段
    [sc removeSegmentAtIndex:3 animated:YES];
    //全部删掉
    //[sc removeAllSegments];
    
    //设置/获取某一段是否有效
    //[sc setEnabled:NO forSegmentAtIndex:1];
    //[sc isEnabledForSegmentAtIndex:1];
    
    //设置背景图片
    [sc setBackgroundImage:[UIImage imageNamed:@"5"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [sc backgroundImageForState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
    //设置间隔图片
    //[sc setDividerImage:[UIImage imageNamed:@"6"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [sc dividerImageForLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
    [sc setTitleTextAttributes:(nullable NSDictionary *)dic forState:UIControlStateNormal];
    

    //添加事件
    [sc addTarget:self action:@selector(sc:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:sc];
    //[sc release];

你可能感兴趣的:(iOS-视图-UISegmentedControl 分栏控件 分段空间 选择条)