【问题标题】:How to change Height of UISegmentedControl's divider如何更改 UISegmentedControl 分隔线的高度
【发布时间】:2018-07-06 18:23:32
【问题描述】:
我在我的项目中使用 UISegmentedControl,我需要更改 UISegmentedControl 中分隔线的高度。
如何做到这一点?
首先我知道我们可以像这样改变分隔线的颜色。
self.setDividerImage(selectedBorderImage, forLeftSegmentState: .normal, rightSegmentState: .selected, barMetrics: .default)
但我需要更改分隔线的高度。
【问题讨论】:
标签:
ios
swift
uisegmentedcontrol
【解决方案1】:
嗯,通过编写它的自定义是可能的。首先,您需要将 UIImage() 空的传递给分隔符
self.setDividerImage(UIImage(), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
然后你为 separator 创建一个视图,给它一个大小,并将它添加为 segmentControl 的子视图。要计算分隔符的位置,您需要在布局已经放置时进行。
var constant: CGFloat = 0
guard let first = self.subviews.first else { return }
#warning("Implementation Custom can be deprecated in the years")
if #available(iOS 13.0, *) {
constant = first.frame.width
} else {
constant = first.frame.minX
}
separator?.removeFromSuperview()
separator = UIView(frame: CGRect(x: constant, y: 5, width: 1, height: 30))
separator?.backgroundColor = .black
self.addSubview(separator.unsafelyUnwrapped)
}