質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

4863閲覧

Failed to render and update auto layout status for のエラーに困っている初心者です

Marcos

総合スコア18

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/07/06 16:15

Xcode10.1を利用していて、掲題Failed to render and update auto layout status forというエラーが発生し、storyboard上でも青枠だけの表示となりつまづいております。

他のサイトを調べ、Derived Dataを削除したり、Podfileをアップデート、Clean bundle等をしてもダメな様です。@IBDesignableを利用してるクラスは下記なのですがご教示頂けますと幸いです。

import UIKit

@IBDesignable class RatingControl: UIStackView {

private var ratingButtons = [UIButton]() var rating = 0{ didSet { updateButtonSelectionStates() }

}

@IBInspectable var starSize: CGSize = CGSize(width: 44.0, height: 44.0){ didSet{ setupButtons() } } @IBInspectable var starCount: Int = 5{ didSet{ setupButtons() } } override init(frame: CGRect) { super.init(frame: frame) setupButtons() } required init(coder: NSCoder) { super.init(coder: coder) setupButtons() } /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code } */ @objc func ratingButtonTapped(button: UIButton){ guard let index = ratingButtons.index(of: button) else { fatalError("The button, (button), is not in the ratingButtons array: (ratingButtons)") } // Calculate the rating of the selected button let selectedRating = index + 1 if selectedRating == rating { // If the selected star represents the current rating, reset the rating to 0. rating = 0 } else { // Otherwise set the rating to the selected star rating = selectedRating } } private func setupButtons() { for button in ratingButtons { removeArrangedSubview(button) button.removeFromSuperview() } ratingButtons.removeAll() let bundle = Bundle(for: type(of: self)) bundle.loadNibNamed(String(describing: self), owner: self, options: nil) 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..<starCount { // Create the button let button = UIButton() button.setImage(emptyStar, for: .normal) button.setImage(filledStar, for: .selected) button.setImage(highlightedStar, for: .highlighted) button.setImage(highlightedStar, for: [.highlighted, .selected]) // Add constraints 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) ratingButtons.append(button) }

}
private func updateButtonSelectionStates() {
for (index, button) in ratingButtons.enumerated() {
// If the index of a button is less than the rating, that button should be selected.
button.isSelected = index < rating
}
}
}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hayabusabusash

2019/07/08 08:54 編集

UIStackViewのカスタムクラスにしていると思いますが、 UIStackViewにしたのには何か理由があったりしますか? もし何か参考にしたリンクなどがあればそちらも見せていただけると嬉しいです! あと完成形は星が横並びになっているようなイメージであっていますか?
guest

回答1

0

ベストアンサー

返信ありがとうございます!
いただいたURLのソースコードと見比べたところ、
以下の部分でエラーが出ているようでした。

Swift

1private func setupButtons() { 2 3 for button in ratingButtons { 4 removeArrangedSubview(button) 5 button.removeFromSuperview() 6 } 7 ratingButtons.removeAll() 8 9 let bundle = Bundle(for: type(of: self)) 10 11 // ここがエラーになっている 12 //bundle.loadNibNamed(String(describing: self), owner: self, options: nil) 13 14 let filledStar = UIImage(named: "filledStar", in: bundle, compatibleWith: self.traitCollection) 15 let emptyStar = UIImage(named: "emptyStar", in: bundle, compatibleWith: self.traitCollection) 16 let highlightedStar = UIImage(named: "highlightedStar", in: bundle, compatibleWith: self.traitCollection) 17 18 for _ in 0..<starCount { 19 // Create the button 20 let button = UIButton() 21 button.setImage(emptyStar, for: .normal) 22 button.setImage(filledStar, for: .selected) 23 button.setImage(highlightedStar, for: .highlighted) 24 button.setImage(highlightedStar, for: [.highlighted, .selected]) 25 26 // Add constraints 27 button.translatesAutoresizingMaskIntoConstraints = false 28 button.heightAnchor.constraint(equalToConstant: starSize.height).isActive = true 29 button.widthAnchor.constraint(equalToConstant: starSize.width).isActive = true 30 31 button.addTarget(self, action: #selector(ratingButtonTapped(button:)), for: .touchUpInside) 32 33 // Add the button to the stack 34 addArrangedSubview(button) 35 ratingButtons.append(button) 36 37 } 38 }

loadNibNamed()はxibファイルを読み込む関数なので、
存在しないファイルを読み込もうとしていてエラーになっていたのではないかなと思います。
こちらの1行をコメントアウトすることで一応動作しました。

念の為使用する画像等(emptyStarなど)がXcodeのプロジェクトに入っていることも確認してください。
ここからダウンロードできるみたいです。

また、
こちらのドキュメントは古い情報のままのようなので最新のSwiftとは違う部分があるかもしれません。

なので最新のSwiftのバージョンに対応した教材で学ぶことをお勧めします。

投稿2019/07/08 11:30

hayabusabusash

総合スコア767

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Marcos

2019/07/08 15:42

ありがとうございます!解決されました! 絶対に挫折しないiphoneアプリを2週してオリジナルアプリに入ってたのですが、知らない事が多すぎてtutorialの知識を使っていました。何かおすすめの教材ございましたらご教示頂けますと幸いです。swift実践入門にて文法は平行して学習しております
hayabusabusash

2019/07/09 00:50

よかったです! 見たところ基本は教材で学ばれているようなので、自分で色んなものを考えて作った方がいいと思います。 作っているうちに出てくるエラーや分からない部分を自分で調べて解決していけば、自然と色んな知識がついていくと思います。 今はたくさんの方(著名なエンジニアの方やライブラリを作った方)がソースコードを公開されているので、 そういった公開されているソースコードを読むこともお勧めです!
Marcos

2019/07/13 10:49

返信遅くなり失礼いたしました。 学習アドバイスありがとうございます!現状ではオリジナルアプリ作成は進めており、その中でチュートリアルで使ったプロセスを導入しておりました。ソースコードの解読も進めてみます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問