refactored naming

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-05-15 11:35:47 -05:00
parent 19a8d8e09a
commit 636b5ad9c0

View File

@ -43,14 +43,11 @@ open class ButtonIcon: Control {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Private Properties // MARK: - Private Properties
//-------------------------------------------------- //--------------------------------------------------
private var iconCenterXConstraint: NSLayoutConstraint? private var centerXConstraint: NSLayoutConstraint?
private var iconCenterYConstraint: NSLayoutConstraint? private var centerYConstraint: NSLayoutConstraint?
private var containerViewWidthConstraint: NSLayoutConstraint? private var layoutGuideWidthConstraint: NSLayoutConstraint?
private var containerViewHeightConstraint: NSLayoutConstraint? private var layoutGuideHeightConstraint: NSLayoutConstraint?
private var containerView = UIView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.backgroundColor = .clear
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Public Properties // MARK: - Public Properties
//-------------------------------------------------- //--------------------------------------------------
@ -90,17 +87,31 @@ open class ButtonIcon: Control {
open override func setup() { open override func setup() {
super.setup() super.setup()
addSubview(containerView)
containerView.addSubview(icon) //create a layoutGuide for the icon to key off of
let iconLayoutGuide = UILayoutGuide()
addLayoutGuide(iconLayoutGuide)
containerView.pinToSuperView() //add the icon
containerViewWidthConstraint = containerView.widthAnchor.constraint(equalToConstant: size.containerSize) addSubview(icon)
containerViewHeightConstraint = containerView.heightAnchor.constraint(equalToConstant: size.containerSize)
//determines the height/width of the icon
layoutGuideWidthConstraint = iconLayoutGuide.widthAnchor.constraint(equalToConstant: size.containerSize)
layoutGuideHeightConstraint = iconLayoutGuide.heightAnchor.constraint(equalToConstant: size.containerSize)
iconCenterXConstraint = icon.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0) //determines the center point of the icon
iconCenterYConstraint = icon.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0) centerXConstraint = icon.centerXAnchor.constraint(equalTo: iconLayoutGuide.centerXAnchor, constant: 0)
centerYConstraint = icon.centerYAnchor.constraint(equalTo: iconLayoutGuide.centerYAnchor, constant: 0)
NSLayoutConstraint.activate([containerViewWidthConstraint!, containerViewHeightConstraint!, iconCenterXConstraint!, iconCenterYConstraint!]) //activate the constraints
NSLayoutConstraint.activate([layoutGuideWidthConstraint!,
layoutGuideHeightConstraint!,
centerXConstraint!,
centerYConstraint!,
iconLayoutGuide.topAnchor.constraint(equalTo: topAnchor),
iconLayoutGuide.bottomAnchor.constraint(equalTo: bottomAnchor),
iconLayoutGuide.leadingAnchor.constraint(equalTo: leadingAnchor),
iconLayoutGuide.trailingAnchor.constraint(equalTo: trailingAnchor)])
} }
open override func reset() { open override func reset() {
@ -120,6 +131,7 @@ open class ButtonIcon: Control {
open override func updateView() { open override func updateView() {
super.updateView() super.updateView()
//ensure there is an icon to set
if let iconName { if let iconName {
icon.name = iconName icon.name = iconName
icon.size = size.value icon.size = size.value
@ -130,40 +142,41 @@ open class ButtonIcon: Control {
icon.reset() icon.reset()
} }
// colors
let bgColor = UIColor.red //backgroundColorConfiguration.getColor(self)
let borderColor = UIColor.green// borderColorConfiguration.getColor(self)
backgroundColor = bgColor
layer.borderColor = borderColor.cgColor
icon.layer.borderColor = UIColor.purple.cgColor
setNeedsLayout() setNeedsLayout()
} }
open override func layoutSubviews() { open override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
let bgColor = UIColor.red //backgroundColorConfiguration.getColor(self)
let borderColor = UIColor.green// borderColorConfiguration.getColor(self)
let borderWidth = 2.0 let borderWidth = 2.0
let cornerRadius = min(frame.width, frame.height) / 2.0 let cornerRadius = min(frame.width, frame.height) / 2.0
// calculate center point for child view with offset // calculate center point for child view with offset
let childCenter = CGPoint(x: center.x + iconOffset.x, y: center.y + iconOffset.y) let childCenter = CGPoint(x: center.x + iconOffset.x, y: center.y + iconOffset.y)
iconCenterXConstraint?.constant = childCenter.x - containerView.center.x centerXConstraint?.constant = childCenter.x - center.x
iconCenterYConstraint?.constant = childCenter.y - containerView.center.y centerYConstraint?.constant = childCenter.y - center.y
// calculate the icon's container size ensuring the padding // calculate the icon's container size ensuring the padding
var iconLayoutSize = size.containerSize
if let customSize { if let customSize {
containerViewWidthConstraint?.constant = CGFloat(customSize) iconLayoutSize = CGFloat(customSize)
containerViewHeightConstraint?.constant = CGFloat(customSize)
} else {
containerViewWidthConstraint?.constant = size.containerSize
containerViewHeightConstraint?.constant = size.containerSize
} }
layoutGuideWidthConstraint?.constant = iconLayoutSize
layoutGuideHeightConstraint?.constant = iconLayoutSize
//container //container
backgroundColor = bgColor
layer.borderColor = borderColor.cgColor
layer.cornerRadius = cornerRadius layer.cornerRadius = cornerRadius
layer.borderWidth = borderWidth layer.borderWidth = borderWidth
//icon //icon
icon.layer.borderColor = UIColor.purple.cgColor icon.layer.borderWidth = borderWidth
icon.layer.borderWidth = 2
} }
} }