适用于iOS的Swift中的自定义分段控件

Geo*_*456 2 uisegmentedcontrol ios swift

我正在尝试实现分段控件,但在布局中看起来很简单.我想自定义它:

  • 例如,失去边界
  • 有一个自定义指标

怎么办呢?我知道在Android中如何自定义TabLayout但在这里我很遗憾.

在此输入图像描述

ElF*_*itz 8

我参加派对已经很晚了,但这就是我要做的事情:

将背景颜色设置为.clear

    segmentedControlInstance.backgroundColor = UIColor(red:0.13, green:0.16, blue:0.29, alpha:1.0)
Run Code Online (Sandbox Code Playgroud)

将背景色调颜色设置为.clear

    segmentedControlInstance.tintColor = .clear
Run Code Online (Sandbox Code Playgroud)

我注意到所选片段的标题是粗体.设置两种状态的文本属性(.normal和.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)
Run Code Online (Sandbox Code Playgroud)

最后,设置两个背景图像.注意,我不知道要为barMetrics参数设置什么:

     segmentedControlInstance.setBackgroundImage(UIImage(name: "selectedSegment", for: .selected, barMetrics: ?)
     segmentedControlInstance.setBackgroundImage(UIImage(name: "normalSegment", for: .normal, barMetrics: ?)
Run Code Online (Sandbox Code Playgroud)

我会让你玩barMetrics参数.