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

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

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

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

Swift

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

Q&A

解決済

1回答

3274閲覧

Swift コードで書いた制約がエラーを起こす

Tololololo

総合スコア118

Xcode

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

Swift

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

0グッド

0クリップ

投稿2018/07/20 12:05

swift4です。

制約をコードで記載したいので元々xib内でつけていた制約を全て削除し、コードで書いてみました。
しかし、エラーが出てしまいます。
おそらく根本的にコードでの書き方を間違えているような気がします。
解決方法を教えてください。

UITableViewとそのcellをxibで作っています。

swift

1 2class TestView: UIView, UITableViewDelegate, UITableViewDataSource { 3 4... 5 6 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 7 /// cell生成 8 let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell", for: indexPath) as! TestTableViewCell 9 10... 11... 12... 13 /// imageViewをCellの左端から16.0の位置で固定したい。しかしERROR。 14 cell.imageView.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor, constant: 16.0).isActive = true 15 16...

画像内に制約は写っていませんが、制約は一つもつけていません。
イメージ説明

エラー内容は下記の通りです。

swift

12018-07-20 20:46:51.193283+0900 Test[1977:743127] [Warning] Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead. 22018-07-20 20:46:51.197165+0900 Test[1977:743127] [LayoutConstraints] Unable to simultaneously satisfy constraints. 3 Probably at least one of the constraints in the following list is one you don't want. 4 Try this: 5 (1) look at each constraint and try to figure out which you don't expect; 6 (2) find the code that added the unwanted constraint or constraints and fix it. 7 (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 8( 9 "<NSAutoresizingMaskLayoutConstraint:0x1d4289970 h=--& v=--& UIImageView:0x105809e90.midX == 30 (active)>", 10 "<NSAutoresizingMaskLayoutConstraint:0x1d42899c0 h=--& v=--& UIImageView:0x105809e90.width == 18 (active)>", 11 "<NSLayoutConstraint:0x1d4289830 UIImageView:0x105809e90.leading == UILayoutGuide:0x1d41ba320'UIViewLayoutMarginsGuide'.leading + 16 (active)>", 12 "<NSAutoresizingMaskLayoutConstraint:0x1d009c020 h=--& v=--& 'UIView-Encapsulated-Layout-Left' UITableViewCellContentView:0x10580cf80.minX == 0 (active, names: '|':Test.TestTableViewCell:0x106031c00'TestTableViewCell' )>", 13 "<NSLayoutConstraint:0x1d42896a0 'UIView-leftMargin-guide-constraint' H:|-(15)-[UILayoutGuide:0x1d41ba320'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UITableViewCellContentView:0x10580cf80 )>" 14) 15 16Will attempt to recover by breaking constraint 17<NSLayoutConstraint:0x1d4289830 UIImageView:0x105809e90.leading == UILayoutGuide:0x1d41ba320'UIViewLayoutMarginsGuide'.leading + 16 (active)> 18 19Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 20The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

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

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

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

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

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

fuzzball

2018/07/23 02:29

なぜxib上で付けないのでしょうか?
guest

回答1

0

ベストアンサー

まず、imageViewのtranslatesAutoresizingMaskIntoConstraintsをオフにすること。

swift

1cell.imageView.translatesAutoresizingMaskIntoConstraints = false

あとは、必要な制約を全て付けて下さい。

投稿2018/07/23 02:29

編集2018/07/23 02:29
fuzzball

総合スコア16731

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

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

Tololololo

2018/07/23 07:52

ありがとうございました ^^
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問