refactored tabs for bugs

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-08-10 15:12:32 -05:00
parent 33062b4b98
commit 4c60e4907c

View File

@ -36,7 +36,7 @@ open class Tabs: View {
}
}
}
//Type of behavior for Scrolling
public enum Overflow: String, CaseIterable {
case scroll
@ -48,7 +48,7 @@ open class Tabs: View {
case medium
case large
}
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
@ -57,13 +57,13 @@ open class Tabs: View {
open var onTabChange: ((Int) -> Void)?
//Determines the layout of the Tabs, defaults to horizontal
open var orientation: Orientation = .horizontal { didSet { if oldValue != orientation { updateTabItems() } } }
open var orientation: Orientation = .horizontal { didSet { if oldValue != orientation { setNeedsUpdate() } } }
///When true, Tabs will have border line. If false is passed then the border line won't be visible.
open var borderLine: Bool = true { didSet { setNeedsUpdate() } }
///It will fill the Tabs to the width of the compoent and all Tabs will be in equal width when orientation is horizontal. This is recommended when there are no more than 2-3 tabs.
open var fillContainer: Bool = false { didSet { updateTabItems() } }
open var fillContainer: Bool = false { didSet { setNeedsUpdate() } }
///When true, Tabs will be sticky to top of page, when orientation is vertical.
open var indicatorFillTab: Bool = false { didSet { setNeedsUpdate() } }
@ -75,22 +75,22 @@ open class Tabs: View {
open var minWidth: CGFloat = 44.0 { didSet { setNeedsUpdate() } }
///If set to 'scroll', Tabs can be overflow and scrollable. With 'none', tabs will not overflow and labels will be wrapped to multiple lines if the label text is long.
open var overflow: Overflow = .scroll { didSet { updateTabItems() } }
open var overflow: Overflow = .scroll { didSet { setNeedsUpdate() } }
///The initial Selected Tab's index and is set once a Tab is clicked
open var selectedIndex: Int = 0 { didSet { setNeedsUpdate() } }
///Determines the size of the Tabs TextStyle
open var size: Size = .medium { didSet { updateTabItems() } }
open var size: Size = .medium { didSet { setNeedsUpdate() } }
///When true, Tabs will be sticky to top of page, when orientation is vertical.
open var sticky: Bool = false { didSet { setNeedsUpdate() } }
///Model of the Tabs you are wanting to show.
open var tabModels: [TabModel] = [] { didSet { updateTabItems() } }
open var tabViews: [Tab] = []
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
@ -107,7 +107,7 @@ open class Tabs: View {
private var borderlineViewHeightConstraint: NSLayoutConstraint?
private var borderlineViewWidthConstraint: NSLayoutConstraint?
private var contentViewWidthConstraint: NSLayoutConstraint?
//--------------------------------------------------
// MARK: - Configuration Properties
//--------------------------------------------------
@ -123,7 +123,7 @@ open class Tabs: View {
if orientation == .horizontal && fillContainer {
return .fillEqually
} else {
return orientation == .horizontal ? .fillProportionally : .fill
return .fill //orientation == .horizontal ? .fillProportionally : .fill
}
}
@ -150,15 +150,15 @@ open class Tabs: View {
public override init(frame: CGRect) {
super.init(frame: frame)
}
public convenience required init() {
self.init(frame: .zero)
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
@ -169,7 +169,7 @@ open class Tabs: View {
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
addSubview(scrollView)
contentView = View()
contentView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(contentView)
@ -180,10 +180,11 @@ open class Tabs: View {
tabStackView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(borderlineView)
contentView.addSubview(tabStackView)
scrollView.pinToSuperView()
contentView.pinToSuperView()
tabStackView.pinToSuperView()
contentView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true
borderlineViewWidthConstraint = borderlineView.widthAnchor.constraint(equalToConstant: 0)
@ -193,21 +194,32 @@ open class Tabs: View {
borderlineViewTrailingConstraint = borderlineView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor)
borderlineViewTopConstraint = borderlineView.topAnchor.constraint(equalTo: contentView.topAnchor)
borderlineViewBottomConstraint = borderlineView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
}
/// Function used to make changes to the View based off a change events or from local properties.
open override func updateView() {
super.updateView()
updateStackView()
updateTabs()
updateContentView()
updateBorderline()
}
//--------------------------------------------------
// MARK: - Private Methods
//--------------------------------------------------
/// Removes all of the Tab Views and creates new ones from the Tab Models property.
private func updateTabItems() {
updateTabItems(with: tabModels)
}
private func updateTabItems(with models: [TabModel]) {
// Clear existing tab items
for tabItem in tabViews {
tabItem.removeFromSuperview()
}
tabViews.removeAll()
// Create new tab items from the models
for model in models {
for model in tabModels {
let tabItem = Tab()
tabItem.size = size
tabItem.text = model.text
@ -224,25 +236,28 @@ open class Tabs: View {
setNeedsUpdate()
scrollToSelectedIndex(animated: false)
}
/// Scrolls to the selected Tab by the selectedIndex.
/// - Parameter animated: If there is animation of the scrolling to the selectedIndex.
private func scrollToSelectedIndex(animated: Bool) {
if orientation == .horizontal && self.overflow == .scroll, selectedIndex < tabViews.count {
let selectedTab = tabViews[selectedIndex]
scrollView.scrollRectToVisible(selectedTab.frame, animated: animated)
}
}
/// Function used to make changes to the View based off a change events or from local properties.
open override func updateView() {
super.updateView()
// Update the stackview properties
/// Updates the StackView from local properties.
private func updateStackView() {
tabStackView.distribution = stackViewDistribution
tabStackView.axis = stackViewAxis
tabStackView.alignment = stackViewAlignment
tabStackView.spacing = stackViewSpacing
// Update tab appearance based on properties
}
/// Updates the Tab individual views from local properties.
private func updateTabs() {
for (index, tabItem) in tabViews.enumerated() {
tabItem.size = size
tabItem.isSelected = selectedIndex == index
@ -254,14 +269,9 @@ open class Tabs: View {
tabItem.indicatorPosition = indicatorPosition
tabItem.accessibilityValue = "\(index+1) of \(tabViews.count) Tabs"
}
//update the width based on rules
updateContentView()
setNeedsLayout()
layoutIfNeeded()
}
// Update the ContentView and ScrollView ContentSize from local properties.
private func updateContentView() {
// Deactivate old constraint
contentViewWidthConstraint?.isActive = false
@ -269,10 +279,10 @@ open class Tabs: View {
// Apply overflow
if orientation == .horizontal && overflow == .scroll && !fillContainer {
let contentWidth = tabStackView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).width
contentViewWidthConstraint = contentView.widthAnchor.constraint(equalToConstant: contentWidth)
contentViewWidthConstraint = nil
scrollView.contentSize = CGSize(width: contentWidth, height: scrollView.bounds.height)
} else {
contentViewWidthConstraint = contentView.widthAnchor.constraint(equalTo: widthAnchor)
contentViewWidthConstraint = contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor)
scrollView.contentSize = bounds.size
}
@ -284,9 +294,8 @@ open class Tabs: View {
scrollToSelectedIndex(animated: true)
}
open override func layoutSubviews() {
super.layoutSubviews()
//update layout for borderline
private func updateBorderline() {
//borderLine
if borderLine {
var edge: UIRectEdge = .bottom
@ -302,21 +311,21 @@ open class Tabs: View {
borderlineViewBottomConstraint?.isActive = false
borderlineViewHeightConstraint?.isActive = false
borderlineViewWidthConstraint?.isActive = false
if edge == .left {
borderlineViewWidthConstraint?.constant = borderlineSize
borderlineViewWidthConstraint?.isActive = true
borderlineViewTopConstraint?.isActive = true
borderlineViewLeadingConstraint?.isActive = true
borderlineViewBottomConstraint?.isActive = true
} else if edge == .top {
borderlineViewHeightConstraint?.constant = borderlineSize
borderlineViewHeightConstraint?.isActive = true
borderlineViewTopConstraint?.isActive = true
borderlineViewLeadingConstraint?.isActive = true
borderlineViewTrailingConstraint?.isActive = true
} else {
borderlineViewHeightConstraint?.constant = borderlineSize
borderlineViewHeightConstraint?.isActive = true
@ -330,4 +339,6 @@ open class Tabs: View {
borderlineView.isHidden = true
}
}
}