【发布时间】:2017-08-26 20:26:10
【问题描述】:
我正在尝试实现分段控件,但它在布局中似乎很简单。我想自定义它:
- 例如失去边界
- 有一个自定义指标
有什么方法可以做到这一点?我知道在 Android 中如何自定义 TabLayout,但老实说我迷路了。
【问题讨论】:
标签: ios swift uisegmentedcontrol
我正在尝试实现分段控件,但它在布局中似乎很简单。我想自定义它:
有什么方法可以做到这一点?我知道在 Android 中如何自定义 TabLayout,但老实说我迷路了。
【问题讨论】:
标签: ios swift uisegmentedcontrol
我迟到了,但我会这样做:
将背景颜色设置为.clear
segmentedControlInstance.backgroundColor = UIColor(red:0.13, green:0.16, blue:0.29, alpha:1.0)
将背景色调设置为 .clear
segmentedControlInstance.tintColor = .clear
我注意到所选片段的标题是粗体的。设置两种状态的文本属性(.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)
最后,设置两个背景图片。注意,我不知道为 barMetrics 参数设置什么:
segmentedControlInstance.setBackgroundImage(UIImage(name: "selectedSegment", for: .selected, barMetrics: ?)
segmentedControlInstance.setBackgroundImage(UIImage(name: "normalSegment", for: .normal, barMetrics: ?)
我会让你玩弄 barMetrics 参数。
【讨论】:
如果我需要自定义的内容不在此列表中,UISegmentedControl Docs,我会自己构建。
在您的图片中,我将使用两个按钮、一个用于指示器的视图以及一些响应用户交互的逻辑。
【讨论】: