// Created by CNTT on 4/27/21.
// Copyright © 2021 Nam Ngọc. All rights reserved.
@IBDesignable class RaitingControl: UIStackView {
private var raitingButton = [UIButton]()
@IBInspectable var starNumber:Int = 5 {
@IBInspectable var starSize:CGSize = CGSize(width: 44.0, height: 44.0){
override init(frame: CGRect) {
required init(coder: NSCoder) {
//Initialization of rating control
func raitingButtonSetting() {
let bundle=Bundle(for: type(of: self))
let nomal=UIImage(named: "Nomal",in:bundle,compatibleWith: .none)
let selected=UIImage(named: "Selected",in:bundle,compatibleWith: .none)
let pressed=UIImage(named: "Pressed",in:bundle,compatibleWith: .none)
for button in raitingButton{
removeArrangedSubview(button)
button.removeFromSuperview()
raitingButton.removeAll()
//•create raitting button
//button.backgroundColor = UIColor.red
button.setImage(nomal, for: .normal)
button.setImage(pressed, for: .highlighted)
button.setImage(selected, for: .selected)
//set width, height attributes
button.heightAnchor.constraint(equalToConstant: starSize.height).isActive=true
button.widthAnchor.constraint(equalToConstant: starSize.width).isActive=true
//set event cactking for the button (bat su kien)
button.addTarget(self, action: #selector(raitingButtonPress(button:)), for: .touchUpInside)
//add to button in stackview
addArrangedSubview(button)
//add to the raitingbutton in array
//raitingButton.append(button)
//raitingButton.append(button)
@objc private func raitingButtonPress(button: UIButton){
if let index=raitingButton.firstIndex(of: button){
print("Chi so cua button la: \(index + 1)")