【发布时间】:2021-01-15 15:10:25
【问题描述】:
我有一个自定义分段控件,其中包含 4 个分段,每个分段中有一个按钮。我不得不对按钮的 contentEdgeInsets 进行更改:
// eg of the second button
customSegmentedControl.secondButton?.contentEdgeInsets = UIEdgeInsets(top: 0, left: -35, bottom: 0, right: 0)
滑块最初使用button.frame.midX 滑动到每个段。更改contentEdgeInsets 后,当滑块滑动到某个段时,它看起来好像在错误的位置,因为标题不再位于按钮的中心。我尝试将滑块更改为滑动到按钮 button.titleLabel:button.titleLabel!.frame.midX 的中心,但是在滑块滑动不正确的地方出现了一个奇怪的错误。
for button in buttons {
UIView.animate(withDuration: 0.2, animations: {
self.slider.frame.origin.x = button.titleLabel!.frame.midX // button.frame.midX was what was originally used
// if it's the selected button break ...
})
}
如何让滑块滑动到按钮titleLabel 的中心?例如,我希望它滑动到 Pencil 这个单词的中间,这是按钮的标题,其 contentEdgeInsets 已更改,而不是按钮本身的中间。
【问题讨论】: