ButtonBarPagerTabStripViewController updateIndicator toIndex 错误

ButtonBarPagerTabStripViewController updateIndicator toIndex is wrong

我需要在用户切换屏幕时添加或删除导航栏中的项目,因此我有如下代码:

override func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) {
    super.updateIndicator(for: viewController, fromIndex: fromIndex, toIndex: toIndex, withProgressPercentage: progressPercentage, indexWasChanged: indexWasChanged)
    if indexWasChanged {
        if toIndex == 2 {
            rightBtn.alpha = 1
        } else {
            rightBtn.alpha = 0
        }
    }
}

rightBtn 只是我导航栏右端的一个按钮。

然而,在我的测试运行期间,我注意到如果用户点击 ButtonBar 而不是滑动来更改屏幕,则 fromIndex 和 toIndex 是错误的。它仍然是上次更改的值。 例如。当用户从第三个屏幕滑动到第二个屏幕时,fromIndex 将为 2,toIndex 为 1,但如果用户点击栏上的第三个按钮,fromIndex 仍将为 2,即使它预计为 1 toIndex也将保持在 1,即使现在它应该是 2。 这会导致我的 rightBtn 不显示,如果用户从第三个屏幕滑动然后点击栏回到第三个。

有没有办法在这种情况下获得正确的 toIndex 或者可以修复它?

第一部分是有问题的部分,我直接点击条形按钮进行导航,第二部分没有指针是通过滑动导航,效果很好,图标应该只出现在第三个选项卡上

我已经找到了解决方案,对于遇到同样问题的任何人,ButtonBarPagerTabStripViewController 中有一个 currentIndex。所以这个:

    if indexWasChanged {
        if currentIndex == 2 {
            rightBtn.alpha = 1
        } else {
            rightBtn.alpha = 0
        }
    }

将对问题中的原始代码进行处理。

我也去了 GitHub project 发现遇到这个问题的其他人已经报告了。希望它能得到修复,两种方法都能奏效。