本文主要是介绍UISegmentedControl基本使用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
UISegmentedControl分段控件基本使用方法。
创建
UISegmentedControl* mySegmentedControl = [[UISegmentedControl alloc]initWithItems:nil];
它不需要指定大小,高度默认为44,不能写0
设置宽度:
[mySegmentedControl setWidth:100 forSegmentAtIndex:0];//设置Item的宽度
设置样式 mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleB
风格可以视使用的场合而定,有三种风格选择,如下:
UISegmentedControlStyleP
UISegmentedControlStyleB
UISegmentedControlStyleB
UISegmentedControlStyleB
添加一个含有图像的片段,用inserSegmentWithImage
[mySegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"pic"] atIndex:3 animated:YES];
示例:
NSArray *segmentedArray = [[NSArrayalloc]initWithObjects:@"学历报表",@"职务报表",nil];
UISegmentedControl * _segmentCtrl = [[UISegmentedContro alloc]initWithItems:segmentedArray];
//用于初始化数组
NSArray *segmentedArray = [[NSArrayalloc]initWithObjects:@"学历报表",@"职务报表",nil];
_segmentCtrl = [[UISegmentedControlalloc]initWithItems:segmentedArray];
_segmentCtrl.frame =CGRectMake(5,0,kMainScreenWidth-10,34);
//设置被选中颜色
_segmentCtrl.tintColor =TABBAR_BARTINT_COLOR;
NSMutableDictionary *barTitleTextAttributes = [NSMutableDictionarydictionary];
[barTitleTextAttributessetObject:CONTENT_FONTforKey:NSFontAttributeName];
[barTitleTextAttributessetObject:CONTENT_COLORforKey:NSForegroundColorAttributeName];
//设置被选择和不选择中时字体与颜色
[_segmentCtrlsetTitleTextAttributes:barTitleTextAttributesforState:UIControlStateNormal];
[_segmentCtrlsetTitleTextAttributes:barTitleTextAttribute forState:UIControlStateSelected];
//设置点击处理函数
[_segmentCtrladdTarget:selfaction:@selector(controlPressed:)forControlEvents:UIControlEventValueChanged];
//设置默认页面
_segmentCtrl.selectedSegmentIndex =0;
-(void)controlPressed:(id)sender
{
UISegmentedControl *control = (UISegmentedControl *)sender;
if (control ==babySegmentCtrl) {
NSInteger x = control.selectedSegmentIndex;
//NSLog(@"select segment index is %d", x);
switch (x) {
case0:{
break;
}
default:
break;
}
}
}
这篇关于UISegmentedControl基本使用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!