Reducing constraints.

This commit is contained in:
Kevin G Christiano 2019-12-02 17:33:06 -05:00
parent 44fd2dffac
commit b443c4a389

View File

@ -7,7 +7,7 @@
// //
@objcMembers open class CheckboxWithLabelView: ViewConstrainingView { @objcMembers open class CheckboxWithLabelView: View {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Outlets // MARK: - Outlets
//-------------------------------------------------- //--------------------------------------------------
@ -33,12 +33,10 @@
// MARK: - Constraints // MARK: - Constraints
//-------------------------------------------------- //--------------------------------------------------
var checkboxWidthConstraint: NSLayoutConstraint?
var checkboxHeightConstraint: NSLayoutConstraint? var checkboxHeightConstraint: NSLayoutConstraint?
var checkboxTopConstraint: NSLayoutConstraint? var checkboxTopConstraint: NSLayoutConstraint?
var checkboxBottomConstraint: NSLayoutConstraint? var checkboxBottomConstraint: NSLayoutConstraint?
var checkboxCenterYConstraint: NSLayoutConstraint? var checkboxCenterYConstraint: NSLayoutConstraint?
var centerLabelCheckboxConstraint: NSLayoutConstraint?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Life Cycle // MARK: - Life Cycle
@ -56,33 +54,24 @@
label.text = "" label.text = ""
let dimension = sizeObject?.getValueBasedOnApplicationWidth() ?? Checkbox.defaultHeightWidth
checkboxWidthConstraint = checkbox.heightAnchor.constraint(equalToConstant: dimension)
checkboxWidthConstraint?.isActive = true
checkboxHeightConstraint = checkbox.widthAnchor.constraint(equalToConstant: dimension)
checkboxHeightConstraint?.isActive = true
checkbox.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true checkbox.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true
checkbox.topAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.topAnchor).isActive = true
layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: checkbox.bottomAnchor).isActive = true
let checboxBottom = checkbox.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
checboxBottom.priority = UILayoutPriority(249)
checboxBottom.isActive = true
// Allows various positions of checkbox.
checkboxBottomConstraint = layoutMarginsGuide.bottomAnchor.constraint(equalTo: checkbox.bottomAnchor) checkboxBottomConstraint = layoutMarginsGuide.bottomAnchor.constraint(equalTo: checkbox.bottomAnchor)
checkboxBottomConstraint?.priority = .defaultHigh
checkboxBottomConstraint?.isActive = true
checkboxTopConstraint = checkbox.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor) checkboxTopConstraint = checkbox.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor)
checkboxTopConstraint?.priority = .defaultHigh
checkboxTopConstraint?.isActive = true
checkboxCenterYConstraint = checkbox.centerYAnchor.constraint(equalTo: centerYAnchor) checkboxCenterYConstraint = checkbox.centerYAnchor.constraint(equalTo: centerYAnchor)
centerLabelCheckboxConstraint = label.centerYAnchor.constraint(equalTo: checkbox.centerYAnchor)
label.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true label.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true
layoutMarginsGuide.trailingAnchor.constraint(equalTo: label.trailingAnchor).isActive = true layoutMarginsGuide.trailingAnchor.constraint(equalTo: label.trailingAnchor).isActive = true
label.leadingAnchor.constraint(equalTo: checkbox.trailingAnchor, constant: PaddingTwo).isActive = true label.leadingAnchor.constraint(equalTo: checkbox.trailingAnchor, constant: PaddingTwo).isActive = true
layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: label.bottomAnchor).isActive = true
let bottomLabelConstraint = layoutMarginsGuide.bottomAnchor.constraint(equalTo: label.bottomAnchor) let bottomLabelConstraint = layoutMarginsGuide.bottomAnchor.constraint(equalTo: label.bottomAnchor)
bottomLabelConstraint.priority = UILayoutPriority(249) bottomLabelConstraint.priority = UILayoutPriority(250)
bottomLabelConstraint.isActive = true bottomLabelConstraint.isActive = true
alignCheckbox(.center) alignCheckbox(.center)
@ -102,7 +91,7 @@
setupView() setupView()
} }
public convenience init() { public convenience override init() {
self.init(frame: .zero) self.init(frame: .zero)
} }
@ -125,19 +114,16 @@
checkboxBottomConstraint?.isActive = false checkboxBottomConstraint?.isActive = false
checkboxTopConstraint?.isActive = false checkboxTopConstraint?.isActive = false
checkboxCenterYConstraint?.isActive = true checkboxCenterYConstraint?.isActive = true
centerLabelCheckboxConstraint?.isActive = true
case .top: case .top:
checkboxBottomConstraint?.isActive = false checkboxBottomConstraint?.isActive = false
checkboxTopConstraint?.isActive = true checkboxTopConstraint?.isActive = true
checkboxCenterYConstraint?.isActive = false checkboxCenterYConstraint?.isActive = false
centerLabelCheckboxConstraint?.isActive = false
case .bottom: case .bottom:
checkboxBottomConstraint?.isActive = true checkboxBottomConstraint?.isActive = true
checkboxTopConstraint?.isActive = false checkboxTopConstraint?.isActive = false
checkboxCenterYConstraint?.isActive = false checkboxCenterYConstraint?.isActive = false
centerLabelCheckboxConstraint?.isActive = false
} }
} }
} }
@ -145,7 +131,7 @@
/// MARK: - Molecular /// MARK: - Molecular
extension CheckboxWithLabelView { extension CheckboxWithLabelView {
override open class func estimatedHeight(forRow json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat { open class func estimatedHeight(forRow json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat {
return CGFloat(Checkbox.defaultHeightWidth) return CGFloat(Checkbox.defaultHeightWidth)
} }
@ -153,29 +139,18 @@ extension CheckboxWithLabelView {
super.updateView(size) super.updateView(size)
label.updateView(size) label.updateView(size)
if self.checkbox.responds(to: #selector(self.updateView(_:))) {
if let dimension = sizeObject?.getValueBased(onSize: size) {
checkboxWidthConstraint?.constant = dimension
checkboxHeightConstraint?.constant = dimension
checkbox.updateView(size)
}
}
layoutIfNeeded() layoutIfNeeded()
} }
override open func alignment() -> UIStackView.Alignment { open func alignment() -> UIStackView.Alignment {
return .leading return .leading
} }
open override func resetConstraints() { open func resetConstraints() {
super.resetConstraints()
checkboxCenterYConstraint?.isActive = false checkboxCenterYConstraint?.isActive = false
checkboxBottomConstraint?.isActive = false checkboxBottomConstraint?.isActive = false
checkboxTopConstraint?.isActive = false checkboxTopConstraint?.isActive = false
centerLabelCheckboxConstraint?.isActive = false
} }
open override func reset() { open override func reset() {