TabView: fix consistency error when closing tabs

This commit is contained in:
James Magahern
2021-02-15 18:40:15 -08:00
parent dbe1377df9
commit 97894f5d61

View File

@@ -9,7 +9,7 @@ import UIKit
class TabView: UIControl
{
var active: Bool = false { didSet { setNeedsLayout() } }
var active: Bool = false { didSet { layoutSubviews() } }
var collapsed: Bool = false
var identifier: UUID?
@@ -153,7 +153,6 @@ class TabBarView: UIView
let identifier = dataSource.tabBarView(self, uniqueIdentifierForTabAtIndex: i)
if let tabView = tabViewsRemoved.first(where: { $0.identifier == identifier }) {
tabViewsRemoved.remove(tabView)
self.reloadTab(atIndex: i)
} else {
let newTabView = makeTabView(withIdentifier: identifier)
if animated { newTabView.collapsed = true }
@@ -170,6 +169,11 @@ class TabBarView: UIView
// Outgoing tabs collapse
tabViewsRemoved.forEach { $0.collapsed = true }
// Reload tabs that are still here
for i in 0..<numberOfTabs {
reloadTab(atIndex: i)
}
UIView.animate(withDuration: 0.22, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: [], animations: { [unowned self] in
layoutSubviews()
}, completion: { [unowned self] finished in
@@ -189,10 +193,13 @@ class TabBarView: UIView
let title = dataSource.tabBarView(self, titleForTabAtIndex: index)
let image = dataSource.tabBarView(self, imageForTabAtIndex: index)
let identifier = dataSource.tabBarView(self, uniqueIdentifierForTabAtIndex: index)
let tabView = tabViews[index]
tabView.label.text = title
tabView.imageView.image = image
if let tabView = visibleTab(atIndex: index) {
tabView.label.text = title
tabView.imageView.image = image
tabView.identifier = identifier
}
}
public func activateTab(atIndex index: Int) {
@@ -200,6 +207,13 @@ class TabBarView: UIView
setNeedsLayout()
}
private func visibleTab(atIndex index: Int) -> TabView? {
let visibleTabs = tabViews.filter { $0.collapsed == false }
if index >= visibleTabs.count { return nil }
return visibleTabs[index]
}
private func makeTabView(withIdentifier identifier: UUID) -> TabView {
let tabView = TabView()
tabView.identifier = identifier
@@ -253,7 +267,8 @@ class TabBarView: UIView
tabWidth = maximumTabWidth
}
for (i, tabView) in tabViews.enumerated() {
var visibleTabIndex = 0
for tabView in tabViews {
tabContainerView.addSubview(tabView)
tabView.alpha = tabView.collapsed ? 0.0 : 1.0
@@ -267,13 +282,15 @@ class TabBarView: UIView
xOffset += tabView.frame.width
if i == activeTabIndex {
if visibleTabIndex == activeTabIndex {
tabView.active = true
} else {
tabView.active = false
}
tabView.layoutIfNeeded()
if !tabView.collapsed { visibleTabIndex += 1 }
}
tabContainerView.contentSize = CGSize(