From 281a10f7e1478f3b249da973c079f4d25e8140fd Mon Sep 17 00:00:00 2001 From: Priya Date: Fri, 17 May 2019 14:36:18 -0400 Subject: [PATCH] Switch molecule --- MVMCoreUI/Molecules/Switch.swift | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/MVMCoreUI/Molecules/Switch.swift b/MVMCoreUI/Molecules/Switch.swift index 658a375c..f756e9d9 100644 --- a/MVMCoreUI/Molecules/Switch.swift +++ b/MVMCoreUI/Molecules/Switch.swift @@ -10,9 +10,11 @@ import UIKit @objcMembers public class Switch: ViewConstrainingView, FormValidationProtocol{ public var mvmSwitch = MVMCoreUISwitch() - var label = Label() - var mfTextButton = MFTextButton() - var isRequired: Bool! = false + public var label = Label() + public var mfTextButton = MFTextButton() + var topConstraint_Switch: NSLayoutConstraint? + var bottomConstraint_textBtn: NSLayoutConstraint? + var isRequired = false var delegateObject: DelegateObject? @objc func switchChanged() { @@ -44,7 +46,7 @@ import UIKit open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) - isRequired = json?[KeyRequired] as? Bool + isRequired = json?[KeyRequired] as? Bool ?? false self.delegateObject = delegateObject if let dict = json?.optionalDictionaryForKey("label") { label.setWithJSON(dict, delegateObject: delegateObject, additionalData: additionalData) @@ -67,19 +69,30 @@ import UIKit if let offKnobColorString = json?.optionalStringForKey("offKnobTintColor") { mvmSwitch.offKnobTintColor = .mfGet(forHex: offKnobColorString) } -// mvmSwitch.isOn = json?.optionalBoolForKey("state") ?? false + // mvmSwitch.isOn = json?.optionalBoolForKey("state") ?? false mvmSwitch.setState(json?.optionalBoolForKey("state") ?? false, animated: true) + //updateContraints() + } + + func updateContraints() { + if let isEmptyText = label.text?.isEmpty,isEmptyText == false{ + topConstraint_Switch?.priority = UILayoutPriority(rawValue:249) + NSLayoutConstraint.constraintPinSubview(mvmSwitch, pinCenterX: false, pinCenterY: true) + } + layoutIfNeeded() } func setupConstraints(forView view: UIView) { NSLayoutConstraint.constraintPinLeftSubview(label, leftConstant: 0) - NSLayoutConstraint.constraintPinSubview(mvmSwitch, pinTop: true, topConstant: 0, topRelation: .equal, pinBottom: true, bottomConstant: 0, bottomRelation: .greaterThanOrEqual, pinLeft: false, leftConstant: 0, leftRelation: .equal, pinRight: true, rightConstant: 0, rightRelation: .equal) + let dict = NSLayoutConstraint.constraintPinSubview(mvmSwitch, pinTop: true, topConstant: 0, topRelation: .greaterThanOrEqual, pinBottom: true, bottomConstant: 0, bottomRelation: .greaterThanOrEqual, pinLeft: false, leftConstant: 0, leftRelation: .equal, pinRight: true, rightConstant: 0, rightRelation: .equal) + topConstraint_Switch = dict?[ConstraintTop] as? NSLayoutConstraint NSLayoutConstraint.constraintPinTopSubview(label, topConstant: 0) _ = NSLayoutConstraint(pinFirstView: label, toSecondView: mvmSwitch, withConstant: PaddingOne, directionVertical: false) _ = NSLayoutConstraint(pinFirstView: label, toSecondView: mfTextButton, withConstant: PaddingOne, directionVertical: true) NSLayoutConstraint.constraintPinLeftSubview(mfTextButton, leftConstant: 0) NSLayoutConstraint.constraintPinBottomSubview(mfTextButton, bottomConstant: 0) - NSLayoutConstraint.constraintPinRightSubview(mfTextButton, rightConstant: 0) + // NSLayoutConstraint.constraintPinRightSubview(mfTextButton, rightConstant: 0) + if label.text?.isEmpty ?? false == false { _ = NSLayoutConstraint(pinFirstView: mvmSwitch, toSecondView: mfTextButton, withConstant: PaddingOne, directionVertical: true) } @@ -108,3 +121,4 @@ import UIKit } +