前提・実現したいこと
掲題の通り、NSLayoutConstraintでItem同士の距離をコードから設定したいです。
どういうことかというと、以下の画像の紫色の四角を、青色の下かつ緑色の右に配置したいです。(サイズでかくてすみません)
発生している問題・エラーメッセージ
addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .top, relatedBy: .equal, toItem: thumbnailImageView, attribute: .bottom, multiplier: 1, constant: 8))
このメソッドを使ってtitleLabelというLabelにAutoLayoutを適応させようとしていますが、画面の最上部に行ってしまいます。
該当のソースコード
Swift
1 2import Foundation 3import UIKit 4 5class VideoCell: UICollectionViewCell { 6 override init(frame: CGRect) { 7 super.init(frame: frame) 8 setupViews() 9 } 10 11 let thumbnailImageView: UIImageView = { 12 let imageView = UIImageView() 13 imageView.backgroundColor = UIColor.blue 14 return imageView 15 }() 16 17 let separaterView: UIView = { 18 let view = UIView() 19 view.backgroundColor = UIColor.gray 20 return view 21 }() 22 23 let userProfileImageView: UIImageView = { 24 let imageView = UIImageView() 25 imageView.backgroundColor = UIColor.green 26 return imageView 27 }() 28 29 let titleLabel: UILabel = { 30 let label = UILabel() 31 label.backgroundColor = UIColor.purple 32 return label 33 }() 34 35 36 37 required init?(coder aDecoder: NSCoder) { 38 fatalError("init(coder:) has not been implemented") 39 } 40 41 func setupViews() { 42 addSubview(thumbnailImageView) 43 addSubview(separaterView) 44 addSubview(userProfileImageView) 45 addSubview(titleLabel) 46 47 addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", view: thumbnailImageView) 48 addConstraintsWithFormat(format: "H:|[v0]|", view: separaterView) 49 addConstraintsWithFormat(format: "H:|-16-[v0(44)]|", view: userProfileImageView) 50 addConstraintsWithFormat(format: "V:|-16-[v0]-8-[v1(44)]-16-[v2(1)]|", view: thumbnailImageView, userProfileImageView ,separaterView) 51 52 // TopConstraints ここがなぜか設定できない 53 addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .top, relatedBy: .equal, toItem: thumbnailImageView, attribute: .bottom, multiplier: 1, constant: 8)) 54 55 // LeftConstraints ここはいけてる 56 addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .left, relatedBy: .equal, toItem: userProfileImageView, attribute: .right, multiplier: 1, constant: 8)) 57 58 // RightConstraints ここもいけてる 59 addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .right, relatedBy: .equal, toItem: thumbnailImageView, attribute: .right, multiplier: 1, constant: 0)) 60 61 addConstraintsWithFormat(format: "V:|[v0(20)]|", view: titleLabel) 62 63 // HeightConstraints これは無理だった 64 // addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0, constant: 20)) 65 } 66} 67 68extension UIView { 69 func addConstraintsWithFormat(format: String, view: UIView...) { 70 var viewDictionary = [String: UIView]() 71 72 for (index, view) in view.enumerated() { 73 let key = "v(index)" 74 view.translatesAutoresizingMaskIntoConstraints = false 75 viewDictionary[key] = view 76 } 77 78 addConstraints(NSLayoutConstraint.constraints( 79 withVisualFormat: format, 80 options: NSLayoutConstraint.FormatOptions(), 81 metrics: nil, 82 views: viewDictionary)) 83 84 } 85} 86
試したこと
・ブレイクポイント貼り付けてメソッド自体が呼ばれてるか調べた。ちゃんと呼ばれてた。
補足情報(FW/ツールのバージョンなど)
Swift4/Xcode10.0
以下の動画を参考にしてます。
https://www.youtube.com/watch?v=3Xv1mJvwXok&list=PL0dzCUj1L5JGKdVUtA5xds1zcyzsz7HLj
回答1件
あなたの回答
tips
プレビュー