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

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

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

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

1209閲覧

addConstraintsWithFormaが定義できません。

退会済みユーザー

退会済みユーザー

総合スコア0

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2018/03/08 08:04

編集2018/03/08 09:04

UIViewを

① func setupViews() { addSubview(AView) addSubview(BView) addConstraintsWithFormat("H:|-16-[v0]-16-|", views: AView) addConstraintsWithFormat("V:|-16-[v0]-16-[v1(1)]|", views: BView) addConstraintsWithFormat("H:|[v0]|", views: BView) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }

上記コード①の様に定義できません。

② addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[v0(1)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": BView]))

上記コード②の様には定義できますが、簡略化したいので①で定義したいです。

Apple Developer Forumsの質問と回答では、
addConstraintsWithFormatはapple's frameworkではないと書いていたりしますが、その理由でしょうか?
swiftでの動画資料を参考に勉強しているので、何故なのかがわかりません。
これは古い定義の方法でしょうか?

よろしくお願いします。

//
下記コードを追記しました。
RUNが成功した場合のシミュレーター画像を追記しました。

イメージ説明

import UIKit class ProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout { override func viewDidLoad() { super.viewDidLoad() navigationItem.title = "プロフ" collectionView?.backgroundColor = .white collectionView?.register(ProfileCell.self, forCellWithReuseIdentifier: "cellId") } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 5 } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: view.frame.width, height: 200) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 0 } class ProfileCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) setupViews() } let profileImageView: UIImageView = { let imageView = UIImageView() imageView.backgroundColor = .blue imageView.translatesAutoresizingMaskIntoConstraints = false return imageView }() let separatorView: UIView = { let view = UIView() view.backgroundColor = .blue view.translatesAutoresizingMaskIntoConstraints = false return view }() func setupViews() { addSubview(profileImageView) addSubview(separatorView) addConstraintsWithFormat("H:|-16-[v0]-16-|", views: profileImageView) addConstraintsWithFormat("V:|-16-[v0]-16-[v1(1)]|", views: separatorView) addConstraintsWithFormat("H:|[v0]|", views: separatorView) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } extension UIView { func addConstraintsWithFormat(format: String, views: UIView...) { var viewsDictionary = [String: UIView]() for (index, view) in views.enumerated() { let key = "v(index)" viewsDictionary[key] = view } addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary)) } } }

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

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

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

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

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

guest

回答1

0

ベストアンサー

ruan65/addConstraintsWithFormat.swift

これかな。(大元かどうかは知らないけど)

投稿2018/03/08 08:31

fuzzball

総合スコア16731

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

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

退会済みユーザー

退会済みユーザー

2018/03/08 08:57

fuzzball様 リンク先を拝見し、同じ様に書き直して見ましたが改善できませんでした。 大元のコード全体を追記しましたので、見ていただけないでしょうか?
fromageblanc

2018/03/08 09:05

UIViewのextensionのブロック、ProfileControllerクラスの外に出す必要ないですか?
fuzzball

2018/03/08 09:12

これも。 func addConstraintsWithFormat(_ format: (以下略)
退会済みユーザー

退会済みユーザー

2018/03/08 09:40

fromageblanc様 fuzzball様 extensionは新たに定義するという事で、ProfileControllerクラスに含まれてはいけなかったのですね。 ありがとうございます。おかげでRUNすることができました。 詳しいご指摘、とても助かりました。 お二人とも、ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問