From 42e482d7272d7a3be854a4e6ba433ab60f3f14d0 Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Wed, 27 Mar 2019 10:46:26 -0400 Subject: [PATCH] Returned addCaretImageView() to the golden path. --- MVMCoreUI/Atoms/Buttons/CaretButton.swift | 50 ++++++++--------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/MVMCoreUI/Atoms/Buttons/CaretButton.swift b/MVMCoreUI/Atoms/Buttons/CaretButton.swift index 393821e8..af7d1d60 100644 --- a/MVMCoreUI/Atoms/Buttons/CaretButton.swift +++ b/MVMCoreUI/Atoms/Buttons/CaretButton.swift @@ -61,7 +61,7 @@ open class CaretButton: MFCustomButton { //------------------------------------------------------ private func changeCaretColor() { - + if let rightCaretView = rightView as? CaretView { rightCaretView.tintColor = isEnabled ? enabledColor : disabledColor } @@ -70,41 +70,27 @@ open class CaretButton: MFCustomButton { private func addCaretImageView() { rightView?.removeFromSuperview() - let edgeInsets: UIEdgeInsets = contentEdgeInsets - - let rightInset = CGFloat(rightViewWidth?.floatValue ?? CaretViewWidth) - - contentEdgeInsets = UIEdgeInsets(top: edgeInsets.top, left: edgeInsets.left, bottom: edgeInsets.bottom, right: 4 + rightInset) - - var caretViewIs: UIView? = rightView - if rightView == nil { - caretViewIs = CaretView() - rightView = caretViewIs - } - - rightView?.translatesAutoresizingMaskIntoConstraints = false - - if let right = rightView { - addSubview(right) - } let width = CGFloat(rightViewWidth?.floatValue ?? CaretViewWidth) + let height = CGFloat(rightViewHeight?.floatValue ?? CaretViewHeight) - if let caretViewIs = caretViewIs { - NSLayoutConstraint(item: caretViewIs, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: width).isActive = true - - let height = CGFloat(rightViewHeight?.floatValue ?? CaretViewHeight) - - NSLayoutConstraint(item: caretViewIs, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: height).isActive = true - - let caretLabelSpacing = NSLayoutConstraint(item: caretViewIs, attribute: .left, relatedBy: .equal, toItem: titleLabel, attribute: .right, multiplier: 1.0, constant: 4.0) - caretLabelSpacing.isActive = true - caretSpacingConstraint = caretLabelSpacing - - NSLayoutConstraint(item: caretViewIs, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true - } + let edgeInsets: UIEdgeInsets = contentEdgeInsets + contentEdgeInsets = UIEdgeInsets(top: edgeInsets.top, left: edgeInsets.left, bottom: edgeInsets.bottom, right: 4 + width) - NSLayoutConstraint(item: self, attribute: .right, relatedBy: .greaterThanOrEqual, toItem: caretViewIs, attribute: .right, multiplier: 1.0, constant: 0).isActive = true + let caretView: UIView = rightView ?? CaretView() + rightView = caretView + rightView?.translatesAutoresizingMaskIntoConstraints = false + addSubview(caretView) + + NSLayoutConstraint(item: caretView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: width).isActive = true + NSLayoutConstraint(item: caretView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: height).isActive = true + + let caretLabelSpacing = NSLayoutConstraint(item: caretView, attribute: .left, relatedBy: .equal, toItem: titleLabel, attribute: .right, multiplier: 1.0, constant: 4.0) + caretLabelSpacing.isActive = true + caretSpacingConstraint = caretLabelSpacing + + NSLayoutConstraint(item: caretView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true + NSLayoutConstraint(item: self, attribute: .right, relatedBy: .greaterThanOrEqual, toItem: caretView, attribute: .right, multiplier: 1.0, constant: 0).isActive = true contentHorizontalAlignment = .left //set correct color after layout changeCaretColor()