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

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

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

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

1回答

5557閲覧

UICollectionViewCellにてUIView-Encapsulated-Layout-Heightが実際のcellの高さより小さく制約エラーが出る問題

yoneapp

総合スコア85

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

0クリップ

投稿2015/03/20 03:01

UICollectionViewLayoutを継承したLayaout実装して、UICollectionViewCellを継承したCellに情報を表示しています。最初は大丈夫なのですが、スクロールを繰り返すうちにAutoLayoutの制約エラーが出ます。

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSLayoutConstraint:0x17469c750 UIImageView:0x1743e5e00.width == 1.33333*UIImageView:0x1743e5e00.height>", "<NSLayoutConstraint:0x17469c340 H:[UIImageView:0x1743e5e00]-(10)-| (Names: '|':UIView:0x174382cb0 )>", "<NSLayoutConstraint:0x17469c390 H:|-(10)-[UIImageView:0x1743e5e00] (Names: '|':UIView:0x174382cb0 )>", "<NSLayoutConstraint:0x17469c3e0 V:|-(10)-[UIImageView:0x1743e5e00] (Names: '|':UIView:0x174382cb0 )>", "<NSLayoutConstraint:0x17469c430 V:[UIImageView:0x1743e5e00]-(10)-[UILabel:0x13f64f2d0'\U51fa\U5ddd\U300c\U5168\U8eab\U306e\U9aa8\U3092\U6298\U3063\U305f\U4ffa\U306b\U3059\U308c\U3070\U3001\U8db3\U306e\U6307\U306a\U3069\U3069\U3046\U3067...']>", "<NSLayoutConstraint:0x17469c520 V:[UILabel:0x13f64ec00]-(10)-| (Names: '|':UIView:0x174382cb0 )>", "<NSLayoutConstraint:0x17469c5c0 V:[UILabel:0x13f64f2d0'\U51fa\U5ddd\U300c\U5168\U8eab\U306e\U9aa8\U3092\U6298\U3063\U305f\U4ffa\U306b\U3059\U308c\U3070\U3001\U8db3\U306e\U6307\U306a\U3069\U3069\U3046\U3067...']-(>=10)-[UILabel:0x13f64ec00]>", "<NSLayoutConstraint:0x17469c1b0 H:|-(0)-[UIView:0x174382cb0] (Names: '|':XXX.ArticleCollectionViewCell:0x13f64e910 )>", "<NSLayoutConstraint:0x17469c660 H:[UIView:0x174382cb0]-(0)-| (Names: '|':XXX.ArticleCollectionViewCell:0x13f64e910 )>", "<NSLayoutConstraint:0x17469c7f0 V:|-(0)-[UIView:0x174382cb0] (Names: '|':XXX.ArticleCollectionViewCell:0x13f64e910 )>", "<NSLayoutConstraint:0x17469c250 V:[UIView:0x174382cb0]-(0)-| (Names: '|':XXX.ArticleCollectionViewCell:0x13f64e910 )>", "<NSLayoutConstraint:0x1708901d0 'UIView-Encapsulated-Layout-Width' H:[XXX.ArticleCollectionViewCell:0x13f64e910(207)]>", "<NSLayoutConstraint:0x170890040 'UIView-Encapsulated-Layout-Height' V:[XXX.ArticleCollectionViewCell:0x13f64e910(102)]>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x17469c750 UIImageView:0x1743e5e00.width == 1.33333*UIImageView:0x1743e5e00.height> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

<NSLayoutConstraint:0x170890040 'UIView-Encapsulated-Layout-Height' V:[XXX.ArticleCollectionViewCell:0x13f64e910(102)]>が私が想定していない制約です。

102の高さは、他のセルが前に使った?高さで、実際のセルの高さは300程度あります。102になることで<NSLayoutConstraint:0x17469c750 UIImageView:0x1743e5e00.width == 1.33333*UIImageView:0x1743e5e00.height>の制約が破綻してしまいます。

UIView-Encapsulated-Layout-HeightがUICollectionViewLayoutのlayoutAttributesForItemAtIndexPathと違うのが原因だと思うのですがUIView-Encapsulated-Layout-Heightはいつ更新されるのでしょうか。

現在は、以下のように代入のタイミングで制約を書き換えるコードを実行しています。この更新タイミングが悪いでしょうか。

警告が出ないようにするためのアドバイスをお願い致します。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as ArticleCollectionViewCell let articleColumn = articleColumns[indexPath.section] cell.style = articleColumn.cellStyles()[indexPath.row] cell.article = articleColumn.articles[indexPath.row] return cell }
var style: ArticleCollectionViewCellStyle = .Text { didSet { switch style { case .Text: self.imageView.addConstraint(self.imageHeightLayoutConstraint) self.imageView.removeConstraint(self.imageAspectRatio43LayoutConstraint) self.imageView.removeConstraint(self.imageAspectRatio169LayoutConstraint) self.imageVerticalLayoutConstraint.constant = 0.0 self.titleLabel.numberOfLines = 3 self.imageView.image = nil case .TopImageFlexible: self.imageView.removeConstraint(self.imageHeightLayoutConstraint) self.imageView.removeConstraint(self.imageAspectRatio43LayoutConstraint) self.imageView.removeConstraint(self.imageAspectRatio169LayoutConstraint) self.imageVerticalLayoutConstraint.constant = 10.0 self.titleLabel.numberOfLines = 4 case .TopImageAspectRatio43: self.imageView.removeConstraint(self.imageHeightLayoutConstraint) self.imageView.addConstraint(self.imageAspectRatio43LayoutConstraint) self.imageView.removeConstraint(self.imageAspectRatio169LayoutConstraint) self.imageVerticalLayoutConstraint.constant = 10.0 self.titleLabel.numberOfLines = 3 case .TopImageAspectRatio169: self.imageView.removeConstraint(self.imageHeightLayoutConstraint) self.imageView.removeConstraint(self.imageAspectRatio43LayoutConstraint) self.imageView.addConstraint(self.imageAspectRatio169LayoutConstraint) self.imageVerticalLayoutConstraint.constant = 10.0 self.titleLabel.numberOfLines = 3 } } }

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

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

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

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

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

guest

回答1

0

ベストアンサー

ArticleCollectionViewCellStyleをsetするときにAutoLayout制約を作り直す、ということをせずに、styleごとに別のCollectionViewCellクラスを作るのが根本的解決かと思います。

現在の実装方法ですと、UICollectionViewCellのサイズが想定していないほど小くなるため制約が破綻するのだとすると、以下の方法で解決するかもしれません。

iOS - UICollectionViewCell.subViewのUnable to simultaneously satisfy constraintsが解決しない場合 - Qiita

投稿2015/03/20 15:17

Mitsuyoshi

総合スコア11

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

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

yoneapp

2015/03/21 13:38

CollectionViewCellを分けるとxibが再利用されないような気がして、このようにしていました。 priorityを下げる件ありがとうございます。そもそも複雑なautolayoutすぎて、priorityを下げたことにより、どこかが破綻してしまったり収拾がつかないのでautolayoutをやめて全部計算して配置することにしました。 ご返答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問