前提・実現したいこと
AutoLayoutのような機能をコード記述で制約をもたせたいのですが、色々なサイトを参考にしながら、NSLayoutAnchorでやるのがよさそうだと思い、サイトを参考にしてコード記述したのですが、うまくいきません。
ビルド前はコードエラーなどはでないのですが、ビルドすると白い画面になってしまいます。
コード記述ミスや、不足があるのかもしれないですが、色々やっても解決できませんでした。
わかる方がいらっしゃいましたら、原因・解決方法など教えて頂けないでしょうか。
宜しくお願い致します。
エラーメッセージ
2018-07-23 23:47:52.888494 AutoLayout[77298:11191984] [LayoutConstraints] 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. ( "<NSLayoutConstraint:0x608000284240 H:|-(0)-[UIScrollView:0x7fca1e00f400] (active, names: '|':UIView:0x7fca1d407860 )>", "<NSLayoutConstraint:0x608000284f10 H:[UIView:0x7fca1d407860]-(0)-[UIScrollView:0x7fca1e00f400] (active)>", "<NSLayoutConstraint:0x618000280fa0 'UIView-Encapsulated-Layout-Width' UIView:0x7fca1d407860.width == 414 (active)>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x608000284f10 H:[UIView:0x7fca1d407860]-(0)-[UIScrollView:0x7fca1e00f400] (active)> 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. 2018-07-23 23:47:52.889410 AutoLayout[77298:11191984] [LayoutConstraints] 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. ( "<NSLayoutConstraint:0x608000284fb0 V:|-(0)-[UIScrollView:0x7fca1e00f400] (active, names: '|':UIView:0x7fca1d407860 )>", "<NSLayoutConstraint:0x6080002850a0 V:[UIView:0x7fca1d407860]-(0)-[UIScrollView:0x7fca1e00f400] (active)>", "<NSLayoutConstraint:0x6180002806e0 'UIView-Encapsulated-Layout-Height' UIView:0x7fca1d407860.height == 736 (active)>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x6080002850a0 V:[UIView:0x7fca1d407860]-(0)-[UIScrollView:0x7fca1e00f400] (active)> 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.
試したこと
iPhone7のサイズで作成して、シュミレーターはiPhone7Plusのサイズで試しています。
サンプルコードでは、scrollViewにNSLayoutAnchorを施しています。
scrollViewにのせているUI部品なども連動してサイズを変更させたいのですが、できませんでした。それは無理なのでしょうか?
Storyboardで制約をもたせて、コードとOutlet接続してから、その他の詳細はコード記述で行う方法をやってみたのですが、scrollViewのみ制約が施され、その他のscrollViewにaddSubviewしている部品などはそのままでした。
コード記述でNSLayoutConstraintも試して見たのですが、同じくscrollViewのみ制約が施されるだけでした。
view, label, button などそれぞれに制約を施すコードを記述する必要があるのでしょうか?
該当のソースコード
// [使用Version Swift3] import UIKit class ViewController: UIViewController,UIScrollViewDelegate { let scrollView = UIScrollView() var scrollViewContentHeight:CGFloat = 667 var labelsMaxY: CGFloat = 100 let titleLabel = UILabel() var labels = [UILabel]() override func viewDidLoad() { super.viewDidLoad() scrollView.frame.size = CGSize(width: 375, height: 667) scrollView.contentSize = CGSize(width: 375, height: scrollViewContentHeight) scrollView.bounces = false scrollView.indicatorStyle = .default scrollView.scrollIndicatorInsets = UIEdgeInsets(top: 10, left: 5, bottom: 10, right: 5) scrollView.delegate = self scrollView.backgroundColor = UIColor.yellow scrollView.translatesAutoresizingMaskIntoConstraints = false self.view.addSubview(scrollView) // NSLayoutAnchor scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true scrollView.leadingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true scrollView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true scrollView.topAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true // タイトルラベル titleLabel.frame = CGRect(x: 20, y: 20, width: 335, height: 20) titleLabel.text = "ラベル" titleLabel.backgroundColor = .magenta scrollView.addSubview(titleLabel) // ラベル量産 for index in 0..<20 { let label = UILabel() label.frame = CGRect(x: 20, y: 100 + (40 * index), width: 335, height: 40) label.backgroundColor = UIColor.lightGray label.text = "test(index + 1)" if index % 2 == 0 {label.backgroundColor = UIColor.gray} labels.append(label) scrollView.addSubview(label) } // 表示されてるラベルのmaxYを取得 var sum: CGFloat = 100 for index in 0..<labels.count { if labels[index].isHidden == false { let height = labels[index].frame.height sum += height labelsMaxY = sum } } // scrollViewContentHeightの値を再設定 scrollViewContentHeight = labelsMaxY + 100 scrollView.contentSize.height = scrollViewContentHeight } //viewDidLoadを閉じる }
■ iPhone7Plusでビルド
NSAnchorを記述した場合 NSAnchorを記述しない場合 Storyboardで制約を施した場合
補足情報(FW/ツールのバージョンなど)
Swift3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/24 06:32
2018/07/24 06:55
2018/07/24 07:19