【问题标题】:UISegmentedControl.noSegment stopped working with Xcode 11, iOS 13 [duplicate]UISegmentedControl.noSegment 停止使用 Xcode 11、iOS 13 [重复]
【发布时间】:2019-10-03 15:09:40
【问题描述】:

我将两个分段控件堆叠在一起,每个控件都有两个选项,因此搜索字段有一个 2x2 的过滤选项网格。这工作正常,但我刚刚更新到 Xcode 11,当我尝试更新它以响应用户选择时,UISegmentedControl.noSegment 已停止工作。但是,当我在属性观察器中将初始值设置为 .noSegment 时,它会起作用。 isMomentary 设置为假。插座都设置正确。我缺少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
    }
}

【问题讨论】:

    标签: ios swift uisegmentedcontrol ios13 xcode11


    【解决方案1】:

    感谢您提出这个问题。我遇到了同样的问题,很高兴得到一些确认,这不仅仅是我遗漏的东西。

    虽然 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
        }
    }
    

    移除和重新插入片段时会有轻微的闪烁,但对我来说,这比损坏的状态更可取。

    编辑

    正如@matt 在下面的评论中指出的那样,只需调用setNeedsLayout,即:

    control.selectSegmentIndex = .noSegment
    control.setNeedsLayout()
    

    【讨论】:

    猜你喜欢
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 2021-01-10
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多