fixed constraint issues

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-08-22 14:26:02 -05:00
parent 57375576ac
commit e88f97e70e
3 changed files with 34 additions and 37 deletions

View File

@ -52,17 +52,13 @@ open class Badge: View {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Constraints // MARK: - Constraints
//-------------------------------------------------- //--------------------------------------------------
private var labelWidthConstraint: NSLayoutConstraint? private var maxWidthConstraint: NSLayoutConstraint?
private var widthConstraint: NSLayoutConstraint { private func updateMaxWidth() {
// Determine which constraint to activate based on the maxWidth and minWidth properties maxWidthConstraint?.isActive = false
if let maxWidth = maxWidth, maxWidth > minWidth { guard let maxWidth else { return }
// Apply maximum width constraint if set and greater than minWidth maxWidthConstraint?.constant = maxWidth
return label.widthAnchor.constraint(lessThanOrEqualToConstant: maxWidth) maxWidthConstraint?.isActive = true
} else {
// Apply minimum width constraint
return label.widthAnchor.constraint(greaterThanOrEqualToConstant: minWidth)
}
} }
//-------------------------------------------------- //--------------------------------------------------
@ -121,8 +117,15 @@ open class Badge: View {
layer.cornerRadius = 2 layer.cornerRadius = 2
addSubview(label) addSubview(label)
label.pinToSuperView(labelInset)
label
.pinTop(labelInset.top)
.pinLeading(labelInset.left)
.pinTrailing(labelInset.right)
.pinBottom(labelInset.bottom, .defaultHigh)
label.widthGreaterThanEqualTo(constant: minWidth)
maxWidthConstraint = label.widthLessThanEqualTo(constant: 0).with { $0.isActive = false }
} }
/// Resets to default settings. /// Resets to default settings.
@ -146,18 +149,14 @@ open class Badge: View {
super.updateView() super.updateView()
updateTextColorConfig() updateTextColorConfig()
updateMaxWidth()
backgroundColor = backgroundColorConfiguration.getColor(self) backgroundColor = backgroundColorConfiguration.getColor(self)
labelWidthConstraint?.isActive = false
labelWidthConstraint = widthConstraint
labelWidthConstraint?.isActive = true
label.textColorConfiguration = textColorConfiguration.eraseToAnyColorable() label.textColorConfiguration = textColorConfiguration.eraseToAnyColorable()
label.numberOfLines = numberOfLines label.numberOfLines = numberOfLines
label.text = text label.text = text
label.surface = surface label.surface = surface
label.disabled = disabled label.disabled = disabled
} }
} }

View File

@ -95,12 +95,11 @@ extension Tabs {
private let layoutGuide = UILayoutGuide() private let layoutGuide = UILayoutGuide()
private var widthConstraint: NSLayoutConstraint? { private func updateWidth() {
if let width, orientation == .vertical { labelWidthConstraint?.isActive = false
return layoutGuide.widthAnchor.constraint(equalToConstant: width) guard let width else { return }
} else { labelWidthConstraint?.constant = width
return layoutGuide.widthAnchor.constraint(greaterThanOrEqualToConstant: minWidth) labelWidthConstraint?.isActive = true
}
} }
//-------------------------------------------------- //--------------------------------------------------
@ -138,16 +137,14 @@ extension Tabs {
layoutGuide.leadingAnchor.constraint(equalTo: leadingAnchor), layoutGuide.leadingAnchor.constraint(equalTo: leadingAnchor),
layoutGuide.trailingAnchor.constraint(equalTo: trailingAnchor)]) layoutGuide.trailingAnchor.constraint(equalTo: trailingAnchor)])
//pin trailing
label.pinTrailing(layoutGuide.trailingAnchor) label.pinTrailing(layoutGuide.trailingAnchor)
labelTopConstraint = label.topAnchor.constraint(equalTo: layoutGuide.topAnchor) //setup constraints
labelTopConstraint?.isActive = true labelWidthConstraint = label.width(constant: 0).with { $0.isActive = false }
labelTopConstraint = label.pinTop(anchor: layoutGuide.topAnchor)
labelBottomConstraint = label.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor) labelLeadingConstraint = label.pinLeading(anchor: layoutGuide.leadingAnchor)
labelBottomConstraint?.isActive = true labelBottomConstraint = label.pinBottom(anchor: layoutGuide.bottomAnchor, priority: .defaultHigh)
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. /// Function used to make changes to the View based off a change events or from local properties.
@ -159,9 +156,7 @@ extension Tabs {
accessibilityIdentifier = "VDSTab:\(text)" accessibilityIdentifier = "VDSTab:\(text)"
//constaints //constaints
labelWidthConstraint?.isActive = false updateWidth()
labelWidthConstraint = widthConstraint
labelWidthConstraint?.isActive = true
labelLeadingConstraint?.constant = leadingSpace labelLeadingConstraint?.constant = leadingSpace
labelTopConstraint?.constant = otherSpace labelTopConstraint?.constant = otherSpace
labelBottomConstraint?.constant = -otherSpace labelBottomConstraint?.constant = -otherSpace

View File

@ -269,8 +269,11 @@ open class TitleLockup: View {
stackView.addArrangedSubview(subTitleLabel) stackView.addArrangedSubview(subTitleLabel)
//pin stackview to edges //pin stackview to edges
stackView.pinToSuperView() stackView
.pinTop()
.pinLeading()
.pinTrailing()
.pinBottom(0, .defaultHigh)
} }
/// Resets to default settings. /// Resets to default settings.