setup contentHugging and added more contraints to toggle when not in label mode
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
10c7435597
commit
3ec2c98425
@ -55,6 +55,7 @@ open class Toggle: Control, Changeable, FormFieldable {
|
|||||||
private var leftConstraints: [NSLayoutConstraint] = []
|
private var leftConstraints: [NSLayoutConstraint] = []
|
||||||
private var rightConstraints: [NSLayoutConstraint] = []
|
private var rightConstraints: [NSLayoutConstraint] = []
|
||||||
private var labelConstraints: [NSLayoutConstraint] = []
|
private var labelConstraints: [NSLayoutConstraint] = []
|
||||||
|
private var toggleConstraints: [NSLayoutConstraint] = []
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Configuration
|
// MARK: - Configuration
|
||||||
@ -148,18 +149,6 @@ open class Toggle: Control, Changeable, FormFieldable {
|
|||||||
|
|
||||||
open var value: AnyHashable? { isOn }
|
open var value: AnyHashable? { isOn }
|
||||||
|
|
||||||
/// The natural size for the receiving view, considering only properties of the view itself.
|
|
||||||
open override var intrinsicContentSize: CGSize {
|
|
||||||
if showLabel {
|
|
||||||
label.sizeToFit()
|
|
||||||
let size = CGSize(width: label.frame.width + spacingBetween + toggleContainerSize.width,
|
|
||||||
height: max(toggleContainerSize.height, label.frame.height))
|
|
||||||
return size
|
|
||||||
} else {
|
|
||||||
return toggleContainerSize
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
open override var shouldHighlight: Bool { false }
|
open override var shouldHighlight: Bool { false }
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -208,6 +197,49 @@ open class Toggle: Control, Changeable, FormFieldable {
|
|||||||
label.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor)
|
label.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
// Set content hugging priority
|
||||||
|
setContentHuggingPriority(.required, for: .horizontal)
|
||||||
|
|
||||||
|
isAccessibilityElement = true
|
||||||
|
if #available(iOS 17.0, *) {
|
||||||
|
accessibilityTraits = .toggleButton
|
||||||
|
} else {
|
||||||
|
accessibilityTraits = .button
|
||||||
|
}
|
||||||
|
addSubview(label)
|
||||||
|
addSubview(toggleView)
|
||||||
|
|
||||||
|
// Set up initial constraints for label and switch
|
||||||
|
toggleView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
|
||||||
|
|
||||||
|
//toggle
|
||||||
|
toggleConstraints = [
|
||||||
|
toggleView.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor),
|
||||||
|
toggleView.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor)
|
||||||
|
]
|
||||||
|
|
||||||
|
//toggle and label variants
|
||||||
|
labelConstraints = [
|
||||||
|
height(constant: toggleContainerSize.height, priority: .defaultLow),
|
||||||
|
heightGreaterThanEqualTo(constant: toggleContainerSize.height, priority: .defaultHigh),
|
||||||
|
label.topAnchor.constraint(equalTo: topAnchor),
|
||||||
|
label.bottomAnchor.constraint(equalTo: bottomAnchor),
|
||||||
|
]
|
||||||
|
|
||||||
|
//label-toggle
|
||||||
|
leftConstraints = [
|
||||||
|
label.leadingAnchor.constraint(equalTo: leadingAnchor),
|
||||||
|
toggleView.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: spacingBetween),
|
||||||
|
toggleView.trailingAnchor.constraint(equalTo: trailingAnchor)
|
||||||
|
]
|
||||||
|
|
||||||
|
//toggle-label
|
||||||
|
rightConstraints = [
|
||||||
|
toggleView.leadingAnchor.constraint(equalTo: leadingAnchor),
|
||||||
|
label.leadingAnchor.constraint(equalTo: toggleView.trailingAnchor, constant: spacingBetween),
|
||||||
|
label.trailingAnchor.constraint(equalTo: trailingAnchor)
|
||||||
|
]
|
||||||
|
|
||||||
bridge_accessibilityValueBlock = { [weak self] in
|
bridge_accessibilityValueBlock = { [weak self] in
|
||||||
guard let self else { return "" }
|
guard let self else { return "" }
|
||||||
if showText {
|
if showText {
|
||||||
@ -261,6 +293,8 @@ open class Toggle: Control, Changeable, FormFieldable {
|
|||||||
label.isHidden = !showLabel
|
label.isHidden = !showLabel
|
||||||
|
|
||||||
if showLabel {
|
if showLabel {
|
||||||
|
NSLayoutConstraint.deactivate(toggleConstraints)
|
||||||
|
|
||||||
label.textAlignment = textPosition == .left ? .right : .left
|
label.textAlignment = textPosition == .left ? .right : .left
|
||||||
label.textStyle = textStyle
|
label.textStyle = textStyle
|
||||||
label.text = statusText
|
label.text = statusText
|
||||||
@ -279,6 +313,7 @@ open class Toggle: Control, Changeable, FormFieldable {
|
|||||||
NSLayoutConstraint.deactivate(leftConstraints)
|
NSLayoutConstraint.deactivate(leftConstraints)
|
||||||
NSLayoutConstraint.deactivate(rightConstraints)
|
NSLayoutConstraint.deactivate(rightConstraints)
|
||||||
NSLayoutConstraint.deactivate(labelConstraints)
|
NSLayoutConstraint.deactivate(labelConstraints)
|
||||||
|
NSLayoutConstraint.activate(toggleConstraints)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user