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
//--------------------------------------------------
private var iconCenterXConstraint: NSLayoutConstraint?
private var iconCenterYConstraint: NSLayoutConstraint?
private var containerViewWidthConstraint: NSLayoutConstraint?
private var containerViewHeightConstraint: NSLayoutConstraint?
private var containerView = UIView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.backgroundColor = .clear
}
private var centerXConstraint: NSLayoutConstraint?
private var centerYConstraint: NSLayoutConstraint?
private var layoutGuideWidthConstraint: NSLayoutConstraint?
private var layoutGuideHeightConstraint: NSLayoutConstraint?
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
@ -90,17 +87,31 @@ open class ButtonIcon: Control {
open override func 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()
containerViewWidthConstraint = containerView.widthAnchor.constraint(equalToConstant: size.containerSize)
containerViewHeightConstraint = containerView.heightAnchor.constraint(equalToConstant: size.containerSize)
//add the icon
addSubview(icon)
//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)
iconCenterYConstraint = icon.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0)
//determines the center point of the icon
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() {
@ -120,6 +131,7 @@ open class ButtonIcon: Control {
open override func updateView() {
super.updateView()
//ensure there is an icon to set
if let iconName {
icon.name = iconName
icon.size = size.value
@ -130,40 +142,41 @@ open class ButtonIcon: Control {
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()
}
open override func layoutSubviews() {
super.layoutSubviews()
let bgColor = UIColor.red //backgroundColorConfiguration.getColor(self)
let borderColor = UIColor.green// borderColorConfiguration.getColor(self)
let borderWidth = 2.0
let cornerRadius = min(frame.width, frame.height) / 2.0
// calculate center point for child view with offset
let childCenter = CGPoint(x: center.x + iconOffset.x, y: center.y + iconOffset.y)
iconCenterXConstraint?.constant = childCenter.x - containerView.center.x
iconCenterYConstraint?.constant = childCenter.y - containerView.center.y
centerXConstraint?.constant = childCenter.x - center.x
centerYConstraint?.constant = childCenter.y - center.y
// calculate the icon's container size ensuring the padding
var iconLayoutSize = size.containerSize
if let customSize {
containerViewWidthConstraint?.constant = CGFloat(customSize)
containerViewHeightConstraint?.constant = CGFloat(customSize)
} else {
containerViewWidthConstraint?.constant = size.containerSize
containerViewHeightConstraint?.constant = size.containerSize
iconLayoutSize = CGFloat(customSize)
}
layoutGuideWidthConstraint?.constant = iconLayoutSize
layoutGuideHeightConstraint?.constant = iconLayoutSize
//container
backgroundColor = bgColor
layer.borderColor = borderColor.cgColor
layer.cornerRadius = cornerRadius
layer.borderWidth = borderWidth
//icon
icon.layer.borderColor = UIColor.purple.cgColor
icon.layer.borderWidth = 2
icon.layer.borderWidth = borderWidth
}
}