From 6b3d9f914398b707eff82551d0e850fcbcf4a0b0 Mon Sep 17 00:00:00 2001 From: John Hampton Date: Mon, 4 Mar 2019 19:52:03 -0500 Subject: [PATCH] Adds property observers --- iOSTemplate/Base.lproj/Main.storyboard | 16 ++++++-- iOSTemplate/RatingControl.swift | 55 +++++++++++++++++++------- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/iOSTemplate/Base.lproj/Main.storyboard b/iOSTemplate/Base.lproj/Main.storyboard index 6460871..3b682d3 100644 --- a/iOSTemplate/Base.lproj/Main.storyboard +++ b/iOSTemplate/Base.lproj/Main.storyboard @@ -18,8 +18,8 @@ - - + + diff --git a/iOSTemplate/RatingControl.swift b/iOSTemplate/RatingControl.swift index ab218be..400d194 100644 --- a/iOSTemplate/RatingControl.swift +++ b/iOSTemplate/RatingControl.swift @@ -7,7 +7,22 @@ import UIKit -class RatingControl: UIStackView { +@IBDesignable class RatingControl: UIStackView { + + // MARK: Properties + private var ratingButtons = [UIButton]() + @IBInspectable var starSize: CGSize = CGSize(width: 44.0, height: 44.0) { + didSet { + setupButtons() + } + } + + @IBInspectable var starCount: Int = 5 { + didSet { + setupButtons() + } + } + var rating = 0 // MARK: Initialization override init(frame: CGRect) { @@ -27,20 +42,32 @@ class RatingControl: UIStackView { // MARK: Private Methods private func setupButtons() { - // Create the button - let button = UIButton() - button.backgroundColor = UIColor.red - - // Add constraints - button.translatesAutoresizingMaskIntoConstraints = false - button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true - button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true - - // Setup the button action - button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchUpInside) + // clear any existing buttons + for button in ratingButtons { + removeArrangedSubview(button) + button.removeFromSuperview() + } + ratingButtons.removeAll() - // Add the button to the stack - addArrangedSubview(button) + for _ in 0..