Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. //
  2. // FeedCell.swift
  3. // FacebookFeed
  4. //
  5. // Created by Piotr Ekert on 05/06/2019.
  6. // Copyright © 2019 Piotr Ekert. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class FeedCell: UICollectionViewCell {
  12.  
  13. override init(frame: CGRect) {
  14. super.init(frame: frame)
  15.  
  16. setupViews()
  17. }
  18.  
  19. required init?(coder aDecoder: NSCoder) {
  20. fatalError("init{:coder} has not been implemented")
  21. }
  22.  
  23. let nameLabel : UILabel = {
  24. let label = UILabel()
  25. label.text = "Sample name"
  26. label.font = UIFont.boldSystemFont(ofSize: 14)
  27. label.translatesAutoresizingMaskIntoConstraints = false
  28.  
  29. return label
  30. }()
  31.  
  32. let profileImageView: UIImageView = {
  33. let imageView = UIImageView()
  34. imageView.contentMode = .scaleAspectFit
  35. imageView.backgroundColor = .red
  36. imageView.translatesAutoresizingMaskIntoConstraints = false
  37.  
  38. return imageView
  39. }()
  40.  
  41. func setupViews() {
  42. backgroundColor = UIColor.white
  43.  
  44. addSubview(nameLabel)
  45. addSubview(profileImageView)
  46.  
  47. //I left code as comments as comparison of two
  48.  
  49. //NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[v0(44)]-8-[v1]-8-|", metrics: nil, views: ["v0":profileImageView, "v1": nameLabel]))
  50. addConstraintWithFormat(format: "H:|-8-[v0(44)]-8-[v1]-8-|", views: profileImageView, nameLabel)
  51. //NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|-8-[v0(44)]-8-|", metrics: nil, views: ["v0":profileImageView]))
  52. addConstraintWithFormat(format: "V:|-8-[v0(44)]-8-|", views: profileImageView)
  53. //NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", metrics: nil, views: ["v0":nameLabel]))
  54. addConstraintWithFormat(format: "V:|[v0]|", views: nameLabel)
  55.  
  56. }
  57. }
  58.  
  59. extension UIView {
  60.  
  61. func addConstraintWithFormat(format: String, views: UIView...) {
  62. var viewsDictionary = [String : UIView]()
  63. for (index, view) in views.enumerated() {
  64. view.translatesAutoresizingMaskIntoConstraints = false
  65. let key = "v\(index)"
  66. viewsDictionary[key] = view
  67. }
  68.  
  69. NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: viewsDictionary))
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement