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