0
1

More than 1 year has passed since last update.

Segmented Controlのセパレーターを消す方法

Posted at

通常Segmented Controlには赤枠で囲んだ仕切り線が付いているので、これを消したい。
だが、Segmented Controlにはそれを隠すようなプロパティはないようだった。
a87997fb2fcdb6c044124f0800d48c99.png

ただ調べるとsetDividerImage(_:forLeftSegmentState:rightSegmentState:barMetrics:)というメソッドがあり、これを使ってセパレーター画像を設定できるらしい。

なので下記のコードでSegmentd Controlの背景と同じ色のセパレーター画像を生成し設定することでを見えないように(背景色と同化)することにした。

.swift
//色はデフォルトのセパレーターと同じ設定
//widthを1にしheightをSegmented Controllerと同じ高さに設定
func removeSegmentSeprator(height: CGFloat) -> UIImage {
     UIGraphicsBeginImageContext(CGSize(width: 1, height: height))

     let context: CGContext = UIGraphicsGetCurrentContext()!
     let backGroundColor = UIColor(red: 238/255, green: 238/255, blue: 239/255, alpha: 1)
     context.setFillColor(backGroundColor.cgColor)
     context.fill(CGRect(x: 0, y: 0, width: 1, height: height))

     guard let image = UIGraphicsGetImageFromCurrentImageContext() else {return UIImage()}
     UIGraphicsEndImageContext()

     return image
}

let image = removeSegmentSeprator(height: segmentControl.bounds.height)
segmentControl.setDividerImage(image, forLeftSegmentState: .normal,rightSegmentState: .normal, barMetrics: .default)

結果的には↓画像のような感じになりうまくいった。👍
8908b4a0bc014bc723469a9efabe8ac2.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1