我最近开始从官方Apple开发者网站学习IOS开发。现在我在3个图像上得到这个错误,我试图用作按钮上的图像。我得到的错误是我试图在变量中投射的3个图像。
@IBInspectable var starSize: CGSize = CGSize(width: 44.0, height: 44.0) {
didSet {
setupButtons()
}
}
@IBInspectable var starCount: Int = 5 {
didSet {
setupButtons()
}
}
//MARK: Intialization
override init(frame: CGRect) {
super.init(frame: frame)
setupButtons()
}
required init(coder: NSCoder) {
super.init(coder: coder)
setupButtons()
}
//MARK: Button Action
@objc func ratingButtonTapped(button: UIButton) {
}
//MARK: Private Methods
private func setupButtons() {
for button in ratingButtons {
removeArrangedSubview(button)
button.removeFromSuperview()
}
ratingButtons.removeAll()
let filledStar_ = UIImage(named: filledStar)
let emptyStar_ = UIImage(named: emptyStar)
let highlightedStar_ = UIImage(named: highlightedStar)
for _ in 0..<starCount {
let button = UIButton()
button.setImage(emptyStar_, for: .normal)
button.setImage(filledStar_, for: .selected)
button.setImage(highlightedStar_, for: .highlighted)
button.setImage(highlightedStar_, for: [.highlighted, .selected])
button.translatesAutoresizingMaskIntoConstraints = false
button.heightAnchor.constraint(equalToConstant: starSize.height).isActive = true
button.widthAnchor.constraint(equalToConstant: starSize.width).isActive = true
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchUpInside)
addArrangedSubview(button)
ratingButtons.append(button)
}
}
答案 0 :(得分:2)
图像名称应为字符串
let filledStar_ = UIImage(named: "filledStar")
let emptyStar_ = UIImage(named: "emptyStar")
let highlightedStar_ = UIImage(named: "highlightedStar")
答案 1 :(得分:0)
我认为图像文字也可能有效。