我有两个相互叠放的分段控件,每个控件都有两个选项,因此搜索字段有一个2x2的过滤选项网格。这项工作正常,但是我刚刚更新为Xcode 11,当我尝试响应用户选择进行更新时,UISegmentedControl.noSegment
已停止工作。但是,当我在属性观察器中将初始值设置为.noSegment
时,它可以工作。 isMomentary
设置为false。插座均已正确设置。我缺少UISegmentedControl
行为的更新,还是一个错误?
显示了here上的新的错误行为。
当前代码在更新之前已经起作用,并且在更新之后停止了工作:
@IBOutlet private weak var segmentedControlOne: UISegmentedControl!
@IBOutlet private weak var segmentedControlTwo: UISegmentedControl! {
didSet {
// Start with no segment selected on this control. This works!
segmentedControlTwo.selectedSegmentIndex = -1
}
}
@IBAction private func oneIndexChanged(_ sender: UISegmentedControl) {
//Turn off selection on second control while first is selected
segmentedControlTwo.selectedSegmentIndex = UISegmentedControl.noSegment
let i = sender.selectedSegmentIndex
if i == 0 {
searchType = .users
} else {
searchType = .contributors
}
}
@IBAction private func twoIndexChanged(_ sender: UISegmentedControl) {
//Turn off selection on first control while second is selected
segmentedControlOne.selectedSegmentIndex = UISegmentedControl.noSegment
let i = sender.selectedSegmentIndex
if i == 0 {
searchType = .articles
} else {
searchType = .categories
}
}
答案 0 :(得分:2)
感谢您提出这个问题。我遇到了同样的问题,因此很高兴得到一些确认,这不仅仅是我所缺少的。
尽管苹果公司希望尽快修复此错误,但我通过重新创建细分来实现以下变通办法。此代码示例基于将图像作为分段的UISegmentedControl
,您当然可以对标题字符串实施相同的方法:
public func resetSegmentedControl(_ control: UISegmentedControl) {
if #available(iOS 13, *) {
// workaround: recreate the segments
let numSegments = control.numberOfSegments
let segmentImages = (0..<numSegments).compactMap { control.imageForSegment(at: $0) }
control.removeAllSegments()
for (index, image) in segmentImages.enumerated() {
control.insertSegment(with: image, at: index, animated: false)
}
} else {
// for earlier versions of iOS, just reset the selectedSegmentIndex
control.selectedSegmentIndex = UISegmentedControl.noSegment
}
}
在移除和重新插入片段时会有轻微的闪烁,但是对我来说,它比破碎的状态更好。
编辑
正如@matt在下面的评论中指出的,所需要做的只是调用setNeedsLayout
,即:
control.selectSegmentIndex = .noSegment
control.setNeedsLayout()