diff --git a/VDS/Components/Tabs/Tab.swift b/VDS/Components/Tabs/Tab.swift index 4526ced0..d33bea63 100644 --- a/VDS/Components/Tabs/Tab.swift +++ b/VDS/Components/Tabs/Tab.swift @@ -20,7 +20,11 @@ extension Tabs { open var index: Int = 0 ///label to write out the text - open var label: Label = Label().with { $0.isUserInteractionEnabled = false } + open var label: Label = Label().with { + $0.isUserInteractionEnabled = false + $0.setContentCompressionResistancePriority(.required, for: .horizontal) + $0.setContentHuggingPriority(.required, for: .horizontal) + } ///orientation of the tabs open var orientation: Tabs.Orientation = .horizontal { didSet { setNeedsUpdate() } } @@ -38,7 +42,7 @@ extension Tabs { open var width: CGFloat? { didSet { setNeedsUpdate() } } ///The text label of the tab. - open var text: String = "" { didSet { setNeedsUpdate() } } + open var text: String = "Tab" { didSet { setNeedsUpdate() } } ///Minimum width for the tab open var minWidth: CGFloat = 44.0 { didSet { setNeedsUpdate() } } @@ -88,12 +92,14 @@ extension Tabs { return VDSLayout.Spacing.space2X.value } } + + private let layoutGuide = UILayoutGuide() - private var widthConstraint: NSLayoutConstraint { + private var widthConstraint: NSLayoutConstraint? { if let width, orientation == .vertical { - return label.widthAnchor.constraint(equalToConstant: width) + return layoutGuide.widthAnchor.constraint(equalToConstant: width) } else { - return label.widthAnchor.constraint(greaterThanOrEqualToConstant: minWidth) + return layoutGuide.widthAnchor.constraint(greaterThanOrEqualToConstant: minWidth) } } @@ -115,35 +121,30 @@ extension Tabs { //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- + open override func setup() { super.setup() + addLayoutGuide(layoutGuide) addSubview(label) accessibilityTraits = .button isAccessibilityElement = true - label.translatesAutoresizingMaskIntoConstraints = false - label.pinTrailing() - - labelTopConstraint = label.topAnchor.constraint(equalTo: topAnchor) - labelTopConstraint?.isActive = true - - labelBottomConstraint = label.bottomAnchor.constraint(equalTo: bottomAnchor) - labelBottomConstraint?.isActive = true - - labelLeadingConstraint = label.leadingAnchor.constraint(equalTo: leadingAnchor) - labelLeadingConstraint?.isActive = true - - let layoutGuide = UILayoutGuide() - addLayoutGuide(layoutGuide) - - labelWidthConstraint = layoutGuide.widthAnchor.constraint(greaterThanOrEqualToConstant: minWidth) - labelWidthConstraint?.isActive = true - //activate the constraints NSLayoutConstraint.activate([layoutGuide.topAnchor.constraint(equalTo: topAnchor), layoutGuide.bottomAnchor.constraint(equalTo: bottomAnchor), layoutGuide.leadingAnchor.constraint(equalTo: leadingAnchor), layoutGuide.trailingAnchor.constraint(equalTo: trailingAnchor)]) + + label.pinTrailing(layoutGuide.trailingAnchor) + + labelTopConstraint = label.topAnchor.constraint(equalTo: layoutGuide.topAnchor) + labelTopConstraint?.isActive = true + + labelBottomConstraint = label.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor) + labelBottomConstraint?.isActive = true + + labelLeadingConstraint = label.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor) + labelLeadingConstraint?.isActive = true } /// Function used to make changes to the View based off a change events or from local properties. @@ -151,13 +152,8 @@ extension Tabs { super.updateView() guard !text.isEmpty else { return } - - //label properties - label.text = text - label.surface = surface - label.textStyle = textStyle - label.textPosition = textPosition - label.textColorConfiguration = textColorConfiguration.eraseToAnyColorable() + + accessibilityIdentifier = "VDSTab:\(text)" //constaints labelWidthConstraint?.isActive = false @@ -166,8 +162,17 @@ extension Tabs { labelLeadingConstraint?.constant = leadingSpace labelTopConstraint?.constant = otherSpace labelBottomConstraint?.constant = -otherSpace - + + //label properties + label.text = text + label.surface = surface + label.textStyle = textStyle + label.textPosition = textPosition + label.textColorConfiguration = textColorConfiguration.eraseToAnyColorable() + setNeedsLayout() + layoutIfNeeded() + } open override func updateAccessibility() { @@ -177,7 +182,7 @@ extension Tabs { open override func layoutSubviews() { super.layoutSubviews() - + removeBorders() if isSelected { @@ -186,5 +191,3 @@ extension Tabs { } } } - -