UISegmentedControl.noSegment停止使用Xcode 11,iOS 13 [英] UISegmentedControl.noSegment stopped working with Xcode 11, iOS 13

查看:66
本文介绍了UISegmentedControl.noSegment停止使用Xcode 11,iOS 13的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个彼此叠放的分段控件,每个控件都有两个选项,因此对于搜索字段有一个2x2的过滤选项网格.这项工作正常,但是我刚刚更新到Xcode 11,并且当我尝试更新以响应用户选择时, UISegmentedControl.noSegment 已停止工作.但是,当我在属性观察器中将初始值设置为 .noSegment 时,它可以工作. isMomentary 设置为false.插座均已正确设置.我是否缺少对 UISegmentedControl 行为的更新,或者这是一个错误?

I've had two segmented controls stacked on top of each other, each with two options, so there's a 2x2 grid of filtering options for a search field. This has worked fine, but I just updated to Xcode 11 and UISegmentedControl.noSegment has stopped working when I try to update it in response to user selection. However, it works when I set the initial value to .noSegment in the property observer. isMomentary is set to false. The outlets are all set up correctly. Is there some update to UISegmentedControl behavior I'm missing, or is this a bug?

此处显示了新的错误行为.

New, incorrect behavior shown here.

当前代码在更新之前已经起作用,并且在更新之后停止了工作:

Current code that was working before, and stopped working after update:

@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
    }
}

推荐答案

感谢您提出这个问题.我遇到了同样的问题,因此很高兴得到一些确认,这不仅仅是我所缺少的.

Thanks for asking this question. I ran into the same issue, so was great to get some confirmation it wasn't just something I was missing.

尽管苹果公司希望尽快修复此错误,但我通过重新创建细分来实现以下变通办法.此代码示例基于以图像为分段的 UISegmentedControl ,您当然可以对标题字符串实施相同的方法:

While Apple hopefully fixes this bug soon, I implemented the following workaround by recreating the segments. This code sample is based on a UISegmentedControl with images as segments, you could of course implement the same approach for title strings:

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

在移除和重新插入片段时会有轻微的闪烁,但是对我来说,它比破碎的状态更好.

There's a slight flicker when removing and re-inserting the segments, but for me that's preferable to the broken state.

编辑

正如@matt在下面的评论中指出的,所需要做的只是调用 setNeedsLayout ,即:

As pointed out by @matt in the comment below, all that's needed is a call to setNeedsLayout,i.e.:

control.selectSegmentIndex = .noSegment
control.setNeedsLayout()

这篇关于UISegmentedControl.noSegment停止使用Xcode 11,iOS 13的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆