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

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

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

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

Q&A

解決済

1回答

1105閲覧

NSLayoutAnchorの使い方

totot

総合スコア20

Swift

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

0グッド

0クリップ

投稿2018/12/13 20:09

NSLayoutAnchorのequalToにこの場合なにを入れればいいのですか?
いろいろ試したものの毎回Use of unresolved identifierと出ます。

import

1 2class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 3 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 navigationItem.title = "Home" 8 9 collectionView?.backgroundColor = UIColor.white 10 11 collectionView?.register(VideoCell.self, forCellWithReuseIdentifier: "cellid") 12 13 } 14 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 15 return 5 16 } 17 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 18 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellid", for: indexPath) 19 20 return cell 21 } 22 23 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 24 return CGSize(width: view.frame.width, height: 200.0) 25 } 26 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 27 return 0 28 } 29 30} 31 32class VideoCell: UICollectionViewCell { 33 override init(frame: CGRect) { 34 super.init(frame: frame) 35 setupView() 36 } 37 38 let thumbnailImageView: UIImageView = { 39 let imageView = UIImageView() 40 imageView.backgroundColor = UIColor.blue 41 imageView.translatesAutoresizingMaskIntoConstraints = false 42 return imageView 43 }() 44 45// let separatorView: UIView = { 46// let view = UIView() 47// view.backgroundColor = UIColor.black 48// view.translatesAutoresizingMaskIntoConstraints = false 49// return view 50// }() 51 52 53 func setupView(){ 54 addSubview(thumbnailImageView) 55// addSubview(separatorView) 56 57 thumbnailImageView.centerXAnchor.constraint(equalTo: .centerXAnchor) 58 59 60 } 61 required init?(coder aDecoder: NSCoder) { 62 fatalError("init(coder:) has not been implemented") 63 } 64}

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

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

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

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

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

guest

回答1

0

ベストアンサー

swift

1NSLayoutConstraint.activate([ 2 thumbnailImageView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor), 3 thumbnailImageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor) 4 thumbnailImageView.widthAnchor.constraint(equalToConstant: 30), 5 thumbnailImageView.heightAnchor.constraint(equalToConstant: 30), 6])

みたいにします。

ちゃんとできてますが、addSubviewよりあとでよぶことと、 .translatesAutoresizingMaskIntoConstraints = false にすることに注意して下さい。

投稿2018/12/14 00:46

ha1f

総合スコア83

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問