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

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

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

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

Q&A

解決済

2回答

3016閲覧

scrollviewに対する制約のかけかた

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

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

0グッド

0クリップ

投稿2018/08/31 06:34

scrollviewを使って、縦方向のスクロールを実現しようとしています。
中身の制約を付けやすくするため、scroll viewの中に一つviewを設けて、その中に配置したい要素を入れていく作りにしようと思っています。
参考URL

ViewController内のコードは以下の通りです。

override func viewDidLoad() { self.view.translatesAutoresizingMaskIntoConstraints = false let scrollView = UIScrollView() scrollView.translatesAutoresizingMaskIntoConstraints = false let baseView = UIView() baseView.translatesAutoresizingMaskIntoConstraints = false self.view.addSubview(scrollView) self.view.addSubview(baseView) scrollView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true scrollView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true scrollView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true scrollView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true baseView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true baseView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true baseView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true baseView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true baseView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true }

ここまでは上手くいっているようなのですが、実際に縦スクロールを実現するため、baseViewに対して画面の高さ以上のheightを指定するとエラーが表示されます。

追加したいコード↓

baseView.heightAnchor.constraint(equalToConstant: 2000).isActive = true

エラー内容↓

2018-08-31 14:30:06.164994+0900 SamplApp[91756:17718038] [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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x60400008ccb0 h=--& v=--& UIView:0x7fb1e7a0ee80.height == 1080 (active)>", "<NSLayoutConstraint:0x60000008e470 UIView:0x7fb1e7a12600.height == 2260 (active)>", "<NSLayoutConstraint:0x60000008c990 V:|-(0)-[UIScrollView:0x7fb1e881ba00] (active, names: '|':UIView:0x7fb1e7a14a10 )>", "<NSLayoutConstraint:0x60000008d1b0 UIScrollView:0x7fb1e881ba00.bottom == UIView:0x7fb1e7a14a10.bottom (active)>", "<NSLayoutConstraint:0x60000008d660 UIView:0x7fb1e7a12600.top == UIScrollView:0x7fb1e881ba00.top (active)>", "<NSLayoutConstraint:0x60000008d750 UIView:0x7fb1e7a12600.bottom == UIScrollView:0x7fb1e881ba00.bottom (active)>", "<NSLayoutConstraint:0x600000090770 V:|-(0)-[UIView:0x7fb1e7a14a10] (active, names: '|':UIView:0x7fb1e7a0ee80 )>", "<NSLayoutConstraint:0x6000000907c0 V:[UIView:0x7fb1e7a14a10]-(0)-| (active, names: '|':UIView:0x7fb1e7a0ee80 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x60000008e470 UIView:0x7fb1e7a12600.height == 2000 (active)>

調べてみたのですが、translatesAutoresizingMaskIntoConstraintsfalseに設定する、という解決方法しか見つからず、困っています。
どうして競合してしまうのか、何かわかる方教えてください。

ちなみに、同じことをstoryboard上で行うと問題なくスクロールします。

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

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

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

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

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

guest

回答2

0

ベストアンサー

すみません、自己解決しました。
scrollViewの子どもとして追加すべきviewを、self.viewに追加していたのが原因でした。

修正後のコード

override func viewDidLoad() { self.view.translatesAutoresizingMaskIntoConstraints = false let scrollView = UIScrollView() scrollView.translatesAutoresizingMaskIntoConstraints = false let baseView = UIView() baseView.translatesAutoresizingMaskIntoConstraints = false self.view.addSubview(scrollView) scrollView.addSubview(baseView) // ここを修正 scrollView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true scrollView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true scrollView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true scrollView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true baseView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true baseView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true baseView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true baseView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true baseView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true }

投稿2018/08/31 06:38

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

とりあえず、

swift

1scrollView.addSubview(baseView)

じゃないですか?

投稿2018/08/31 06:40

fuzzball

総合スコア16731

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

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

fuzzball

2018/08/31 06:40

ま、負けたw
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問