FoodTrackerのチュートリアルでつまずいてしまったのでアドバイスお願いします。
環境
xcode 9.4.1
困っていること
以下のように☆を均等に並べたいのですが画像のようになっていまいます。
コード
// RatingControl.swift import UIKit @IBDesignable class RatingControl: UIStackView { //MARK: Properties private var ratingButtons = [UIButton]() var rating = 0 @IBInspectable var starSize: CGSize = CGSize(width: 44.0, height: 44.0) { didSet { setupButtons() } } @IBInspectable var starCount: Int = 5 { didSet { setupButtons() } } //MARK: Initialization(初期化) override init(frame: CGRect) { super.init(frame: frame) setupButtons() } required init(coder: NSCoder) { super.init(coder: coder) setupButtons() } //MARK: Private Methods private func setupButtons() { for button in ratingButtons { removeArrangedSubview(button) button.removeFromSuperview() } ratingButtons.removeAll() //ボタン画像読み込み let bundle = Bundle(for: type(of: self)) let filledStar = UIImage(named: "filledStar", in: bundle, compatibleWith: self.traitCollection) let emptyStar = UIImage(named:"emptyStar", in: bundle, compatibleWith: self.traitCollection) let highlightedStar = UIImage(named:"highlightedStar", in: bundle, compatibleWith: self.traitCollection) for _ in 0..<5 { // ボタン作成 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) // Add the button to the stack addArrangedSubview(button) // Add the new button to the rating button array ratingButtons.append(button) } } //MARK: Button Action @objc func ratingButtonTapped(button: UIButton) { print("Button pressed ????") } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。