From a682e4321da105afadbe82250d525d06b73ae525 Mon Sep 17 00:00:00 2001 From: Simon Mitchell Date: Fri, 18 Dec 2020 20:44:16 +0000 Subject: [PATCH 1/2] Adds imageSizeRatio property to `BadgeView` --- Sources/Views/Badges/BadgeView.swift | 36 +++++++++++++++++++--------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/Sources/Views/Badges/BadgeView.swift b/Sources/Views/Badges/BadgeView.swift index bbbf7de..9b7fa36 100644 --- a/Sources/Views/Badges/BadgeView.swift +++ b/Sources/Views/Badges/BadgeView.swift @@ -11,13 +11,6 @@ import UIKit /// `UIView` for displaying a badge. open class BadgeView: BadgeContainerView { - /// Fixed constants for `BadgeView` - private struct Constants { - - /// Width of `imageView` subview relative to `self` - static let imageViewWidthScale: CGFloat = 0.65 - } - /// `UIImageView` subview public private(set) lazy var imageView: UIImageView = { let imageView = UIImageView() @@ -26,9 +19,25 @@ open class BadgeView: BadgeContainerView { imageView.contentMode = .scaleAspectFit return imageView }() + + /// The constraint between the image view's width and the + /// container view's width. Can be adjusted to change how much + /// of the container the image fills + private var imageWidthConstraint: NSLayoutConstraint? // MARK: - Computed + /// Width of `imageView` subview relative to `self` + public var imageSizeRatio: CGFloat = 0.65 { + didSet { + if let widthConstraint = imageWidthConstraint { + imageView.removeConstraint(widthConstraint) + } + setupImageWidthConstraint() + setNeedsUpdateConstraints() + } + } + /// Allows setting the rendering mode of the image view, /// defaults to `.alwaysTemplate` public var renderingMode: UIImage.RenderingMode = .alwaysTemplate { @@ -79,14 +88,19 @@ open class BadgeView: BadgeContainerView { private func setup() { addSubview(imageView) imageView.translatesAutoresizingMaskIntoConstraints = false + setupImageWidthConstraint() NSLayoutConstraint.activate([ imageView.centerXAnchor.constraint(equalTo: centerXAnchor), imageView.centerYAnchor.constraint(equalTo: centerYAnchor), - imageView.widthAnchor.constraint( - equalTo: widthAnchor, multiplier: - Constants.imageViewWidthScale - ), imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor) ]) } + + private func setupImageWidthConstraint() { + imageWidthConstraint = imageView.widthAnchor.constraint( + equalTo: widthAnchor, + multiplier: imageSizeRatio + ) + imageWidthConstraint?.isActive = true + } } From 6bfc1ca5c958695b59cefd6e69c9f59525ec8a72 Mon Sep 17 00:00:00 2001 From: Simon Mitchell Date: Mon, 1 Feb 2021 11:33:14 +0000 Subject: [PATCH 2/2] Makes PR changes --- Sources/Views/Badges/BadgeView.swift | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Sources/Views/Badges/BadgeView.swift b/Sources/Views/Badges/BadgeView.swift index 9b7fa36..ff12060 100644 --- a/Sources/Views/Badges/BadgeView.swift +++ b/Sources/Views/Badges/BadgeView.swift @@ -23,17 +23,14 @@ open class BadgeView: BadgeContainerView { /// The constraint between the image view's width and the /// container view's width. Can be adjusted to change how much /// of the container the image fills - private var imageWidthConstraint: NSLayoutConstraint? + private var imageWidthConstraint: NSLayoutConstraint! // MARK: - Computed /// Width of `imageView` subview relative to `self` public var imageSizeRatio: CGFloat = 0.65 { didSet { - if let widthConstraint = imageWidthConstraint { - imageView.removeConstraint(widthConstraint) - } - setupImageWidthConstraint() + updateImageWidthConstraint() setNeedsUpdateConstraints() } } @@ -88,7 +85,7 @@ open class BadgeView: BadgeContainerView { private func setup() { addSubview(imageView) imageView.translatesAutoresizingMaskIntoConstraints = false - setupImageWidthConstraint() + updateImageWidthConstraint() NSLayoutConstraint.activate([ imageView.centerXAnchor.constraint(equalTo: centerXAnchor), imageView.centerYAnchor.constraint(equalTo: centerYAnchor), @@ -96,7 +93,10 @@ open class BadgeView: BadgeContainerView { ]) } - private func setupImageWidthConstraint() { + private func updateImageWidthConstraint() { + if let widthConstraint = imageWidthConstraint { + imageView.removeConstraint(widthConstraint) + } imageWidthConstraint = imageView.widthAnchor.constraint( equalTo: widthAnchor, multiplier: imageSizeRatio