我将两个分段控件堆叠在一起,每个控件都有两个选项,因此搜索字段有一个 2x2 的过滤选项网格。这工作得很好,但我刚刚更新到 Xcode 11 并且UISegmentedControl.noSegment
当我尝试根据用户选择更新它时,它已停止工作。但是,当我将初始值设置为.noSegment
在属性观察器中。isMomentary
设置为 false。插座均已正确设置。有更新吗UISegmentedControl
我缺少的行为,或者这是一个错误?
显示新的、不正确的行为here https://gph.is/g/aeYdQ7W.
当前代码之前可以工作,但更新后停止工作:
@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
}
}
感谢您提出这个问题。我遇到了同样的问题,所以很高兴得到一些确认,这不仅仅是我所缺少的。
虽然 Apple 希望尽快修复此错误,但我通过重新创建段实现了以下解决方法。该代码示例基于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
}
}
移除和重新插入片段时会出现轻微的闪烁,但对我来说,这比损坏的状态更好。
EDIT
正如 @matt 在下面的评论中指出的,所需要的只是调用setNeedsLayout
,i.e.:
control.selectSegmentIndex = .noSegment
control.setNeedsLayout()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)