fix for button

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-08-12 09:35:16 -05:00
parent f396718e58
commit d4d1b8d7c9

View File

@ -22,9 +22,6 @@ open class Button: ButtonBase, Useable {
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
private var minWidthConstraint: NSLayoutConstraint?
private var widthConstraint: NSLayoutConstraint?
private var heightConstraint: NSLayoutConstraint?
private var initialSetupPerformed = false
//--------------------------------------------------
@ -36,7 +33,18 @@ open class Button: ButtonBase, Useable {
open var size: ButtonSize = .large { didSet { setNeedsUpdate() }}
open var width: CGFloat? { didSet { setNeedsUpdate() }}
private var _width: CGFloat? = nil
open var width: CGFloat? {
get { _width }
set {
if let newValue, newValue > size.minimumWidth {
_width = newValue
} else {
_width = nil
}
setNeedsUpdate()
}
}
open override var textColor: UIColor {
textColorConfiguration.getColor(self)
@ -120,14 +128,6 @@ open class Button: ButtonBase, Useable {
super.setup()
isAccessibilityElement = true
accessibilityTraits = .button
//only 1 of the 2 widths can be on at the same time
widthConstraint = widthAnchor.constraint(equalToConstant: 0)
minWidthConstraint = widthAnchor.constraint(greaterThanOrEqualToConstant: size.minimumWidth)
//height
heightConstraint = heightAnchor.constraint(equalToConstant: 0)
heightConstraint?.isActive = true
}
/// Resets to default settings.
@ -145,7 +145,12 @@ open class Button: ButtonBase, Useable {
// MARK: - Overrides
//--------------------------------------------------
open override var intrinsicContentSize: CGSize {
guard let width, width > 0 else { return super.intrinsicContentSize }
guard let width, width > 0 else {
var superSize = super.intrinsicContentSize
superSize.height = size.height
return superSize
}
return CGSize(width: width > size.minimumWidth ? width : size.minimumWidth, height: size.height)
}
@ -156,9 +161,7 @@ open class Button: ButtonBase, Useable {
let bgColor = backgroundColorConfiguration.getColor(self)
let borderColor = borderColorConfiguration.getColor(self)
let borderWidth = use == .secondary ? VDSFormControls.widthBorder : 0.0
let buttonHeight = size.height
let cornerRadius = size.cornerRadius
let minWidth = size.minimumWidth
let edgeInsets = size.edgeInsets
backgroundColor = bgColor
@ -166,18 +169,8 @@ open class Button: ButtonBase, Useable {
layer.cornerRadius = cornerRadius
layer.borderWidth = borderWidth
contentEdgeInsets = edgeInsets
minWidthConstraint?.constant = minWidth
heightConstraint?.constant = buttonHeight
if let width, width > minWidth {
widthConstraint?.constant = width
widthConstraint?.isActive = true
minWidthConstraint?.isActive = false
} else {
widthConstraint?.isActive = false
minWidthConstraint?.isActive = true
}
invalidateIntrinsicContentSize()
}
}