2017-03-19 24 views
1

我試圖實現分段控件,但它在佈局中看起來很簡單。我想自定義:Swift for iOS中的自定義分段控制

  • 失去邊界例如
  • 有一個自定義指標

會是怎樣做到這一點的呢?我知道在Android中如何自定義TabLayout,但在這裏我失去了誠實。

enter image description here

回答

0

如果我需要定製沒有這份名單,UISegmentedControl Docs上,我會建立自己的。

在您的圖片中,我會使用兩個按鈕,一個指示器視圖和一些邏輯來響應用戶交互。

2

我很遲到了,但在這裏就是我想要做的:

設置背景顏色爲.clear

segmentedControlInstance.backgroundColor = UIColor(red:0.13, green:0.16, blue:0.29, alpha:1.0) 

設置背景色色.clear

segmentedControlInstance.tintColor = .clear 

我注意到所選段的標題是粗體。設置兩種狀態的文本屬性(。中性& .selected)

segmentedControlInstance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)], for: .normal) 
    segmentedControlInstance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 16)], for: .selected) 

最後,設置兩個背景圖像。注意,我不知道該怎麼對barMetrics參數設置:

 segmentedControlInstance.setBackgroundImage(UIImage(name: "selectedSegment", for: .selected, barMetrics: ?) 
    segmentedControlInstance.setBackgroundImage(UIImage(name: "normalSegment", for: .normal, barMetrics: ?) 

我會讓你玩的barMetrics參數。