您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

UISegmentedControl 分段控件用法大全:從創建到圓角美化

編輯:iOS 相關教學

UISegmentedControl 相當於桌面系統裡的單選按鈕組。由於手機屏幕狹窄,它的選項數量不宜過多,通常在二到四個之間最合適——比如「全部 / 待付款 / 已完成」這樣的篩選切換,或者替換導航欄標題的 segmented 樣式。

一、創建

UISegmentedControl *mySegmentedControl = [[UISegmentedControl alloc] initWithItems:nil];

它的初始化方法只有 initWithItems:,並沒有 initWithFrame:,所以位置和大小不必在創建時指定,後續通過 frame 或 Auto Layout 約束即可。若要單獨控制某一段的寬度:

[mySegmentedControl setWidth:100 forSegmentAtIndex:0];

二、風格設置

mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

typedef enum {
    UISegmentedControlStylePlain,     // 帶灰邊的大號按鈕,適合偏好設置
    UISegmentedControlStyleBordered,  // 黑邊大號按鈕,適合表格單元格
    UISegmentedControlStyleBar,       // 小號按鈕,適合導航欄
    UISegmentedControlStyleBezeled    // 立體凸起風格
} UISegmentedControlStyle;

使用 Bar 風格時,可以通過 tintColor 給整個控件上色:

UIColor *myTint = [[UIColor alloc] initWithRed:0.66 green:1.0 blue:0.77 alpha:1.0];
mySegmentedControl.tintColor = myTint;

三、添加與刪除片段

每一段都是一個獨立按鈕,可以承載文字或圖片。用戶同一時刻只能選中其中一段。

[mySegmentedControl insertSegmentWithTitle:@"First"  atIndex:0 animated:YES];
[mySegmentedControl insertSegmentWithTitle:@"Second" atIndex:1 animated:YES];
[mySegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"pic"] atIndex:2 animated:YES];

// 刪除
[mySegmentedControl removeSegmentAtIndex:0 animated:YES];
[mySegmentedControl removeAllSegments];

四、標題與圖像

[mySegmentedControl setTitle:@"ZERO" forSegmentAtIndex:0];
NSString *myTitle = [mySegmentedControl titleForSegmentAtIndex:1];

[mySegmentedControl setImage:[UIImage imageNamed:@"pic"] forSegmentAtIndex:1];
UIImage *myImage = [mySegmentedControl imageForSegmentAtIndex:2];

要注意圖片不會自動縮放,多大就顯示多大,所以切圖尺寸必須精確,否則容易出現文字或圖形被裁切。

五、選中行為

默認情況下,某一段被選中後會一直保持高亮,直到另一段被點擊。如果希望按一下就「彈起」(類似瞬時按鈕),打開 momentary:

mySegmentedControl.momentary = YES;

副作用是:開啟後點擊不會更新 selectedSegmentIndex,也就無法通過這個屬性得知當前選中項。需要在回調中自行處理。

設置默認選中項(索引從 0 開始):

mySegmentedControl.selectedSegmentIndex = 0;

六、顯示與讀取

[parentView addSubview:mySegmentedControl];              // 加到普通視圖
self.navigationItem.titleView = mySegmentedControl;        // 加到導航欄

int x = mySegmentedControl.selectedSegmentIndex;           // 讀取當前選中索引

七、事件響應

[mySegmentedControl addTarget:self
                       action:@selector(selected:)
             forControlEvents:UIControlEventValueChanged];

- (void)selected:(id)sender {
    UISegmentedControl *control = (UISegmentedControl *)sender;
    switch (control.selectedSegmentIndex) {
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        default:
            break;
    }
}

八、圓角與去高亮的實用寫法

系統默認樣式在現代扁平化設計裡往往顯得突兀。通過 CALayer 與 titleTextAttributes 可以把它調成自定義風格:

UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:
                             @[@"設置", @"知道了"]];
seg.frame = CGRectMake(10, 0, CGRectGetWidth(self.view.frame) - 20, 35);
seg.center = isbluetoothOffAlerView.center;

seg.layer.borderColor = [UIColor whiteColor].CGColor;
seg.layer.borderWidth = 2;
seg.tintColor = [UIColor whiteColor];
seg.backgroundColor = [UIColor clearColor];

NSDictionary *dic = @{
    NSForegroundColorAttributeName: [UIColor whiteColor],
    NSFontAttributeName: [UIFont systemFontOfSize:24]
};
[seg setTitleTextAttributes:dic forState:UIControlStateNormal];

seg.layer.cornerRadius = 15;
seg.layer.masksToBounds = YES;

實踐小結

使用 UISegmentedControl 時有幾個反覆出現的坑:一是 iOS 13 之後蘋果推薦使用 UISegmentedControl 的新初始化方式並逐步淡化了 segmentedControlStyle,新項目可以直接依賴外觀默認值;二是文字過長時會被截斷,記得動態計算寬度或縮小字號;三是它並不是真正的「單選框」,選中狀態需要自己在回調中維護數據模型,避免依賴控件內部狀態造成邏輯不一致。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved