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)>
調べてみたのですが、translatesAutoresizingMaskIntoConstraints
をfalse
に設定する、という解決方法しか見つからず、困っています。
どうして競合してしまうのか、何かわかる方教えてください。
ちなみに、同じことをstoryboard上で行うと問題なくスクロールします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。