Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FeedCell.swift
- // FacebookFeed
- //
- // Created by Piotr Ekert on 05/06/2019.
- // Copyright © 2019 Piotr Ekert. All rights reserved.
- //
- import UIKit
- class FeedCell: UICollectionViewCell {
- override init(frame: CGRect) {
- super.init(frame: frame)
- setupViews()
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init{:coder} has not been implemented")
- }
- let nameLabel : UILabel = {
- let label = UILabel()
- label.text = "Sample name"
- label.font = UIFont.boldSystemFont(ofSize: 14)
- label.translatesAutoresizingMaskIntoConstraints = false
- return label
- }()
- let profileImageView: UIImageView = {
- let imageView = UIImageView()
- imageView.contentMode = .scaleAspectFit
- imageView.backgroundColor = .red
- imageView.translatesAutoresizingMaskIntoConstraints = false
- return imageView
- }()
- func setupViews() {
- backgroundColor = UIColor.white
- addSubview(nameLabel)
- addSubview(profileImageView)
- //I left code as comments as comparison of two
- //NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[v0(44)]-8-[v1]-8-|", metrics: nil, views: ["v0":profileImageView, "v1": nameLabel]))
- addConstraintWithFormat(format: "H:|-8-[v0(44)]-8-[v1]-8-|", views: profileImageView, nameLabel)
- //NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|-8-[v0(44)]-8-|", metrics: nil, views: ["v0":profileImageView]))
- addConstraintWithFormat(format: "V:|-8-[v0(44)]-8-|", views: profileImageView)
- //NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", metrics: nil, views: ["v0":nameLabel]))
- addConstraintWithFormat(format: "V:|[v0]|", views: nameLabel)
- }
- }
- extension UIView {
- func addConstraintWithFormat(format: String, views: UIView...) {
- var viewsDictionary = [String : UIView]()
- for (index, view) in views.enumerated() {
- view.translatesAutoresizingMaskIntoConstraints = false
- let key = "v\(index)"
- viewsDictionary[key] = view
- }
- NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: viewsDictionary))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement