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)) } } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/03/08 08:57
2018/03/08 09:05
2018/03/08 09:12
退会済みユーザー
2018/03/08 09:40