UISegmentedControl是分段控件,當用戶輸入的不單單是布爾值時,使用。分段控件提供一欄按鈕,有時稱按鈕欄,可是隻能激活其中一個按鈕。分段控件會時用戶在屏幕上看到的內容發生變化,經常用於不一樣類別信息之間的選擇 ,或者不一樣界面之間的切換。字體
1.首先建立 界面數spa
NSArray *segmentedArray = [[NSArray alloc] initWithObjects:@「1」,@「2」, nil];3d
2.建立 UIsegmentedControl orm
//初始化UISegmentedControl索引
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedArray];事件
設置frame圖片
segmentedControl.frame = CGRectMake(0, 0, 180, 25);get
設置默認選擇項索引it
segmentedControl.selectedSegmentIndex = 0;io
segmentedControl.tintColor = [UIColor redColor];
//式樣
segmentedControl.segmentedControlStyle
UISegmentedControlStylePlain, // large plain
UISegmentedControlStyleBordered, // large bordered
UISegmentedControlStyleBar, // small button/nav bar style. tintable
UISegmentedControlStyleBezeled,
//設置指定索引選項的寬度
[segmentedControl setWidth:100 forSegmentAtIndex:i];
//設置指定索引的題目
[segmentedControl setTitle:segmentedArray[i] forSegmentAtIndex:i];
//設置title字體大小,及顏色
[segmentedControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
//設置選中下標
segmentedControl.selectedSegmentIndex = 0;
//設置到導航欄上
self.navigationItem.titleView = segmentedControl;
//插入指定索引項目不設置題目
[segmentedControl insertSegmentWithTitle: atIndex: animated:];
//設置圖片
[segmentedControl insertSegmentWithImage: atIndex: animated:];
//設置指定選項不可選
[segmentedControl setEnabled:NO forSegmentAtIndex:];
//添加點擊事件改變界面
[segmentedControl addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventValueChanged];
小知識,大用途,誰用誰知道。