Skip to content

Instantly share code, notes, and snippets.

@markhorrocks
Last active July 6, 2023 19:17
Show Gist options
  • Select an option

  • Save markhorrocks/0c366f06cdad8ef92871e2591bf8178d to your computer and use it in GitHub Desktop.

Select an option

Save markhorrocks/0c366f06cdad8ef92871e2591bf8178d to your computer and use it in GitHub Desktop.

Revisions

  1. markhorrocks revised this gist Dec 19, 2016. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,13 @@
    This is a collection view of vertically scrolling cells 1/3 high as wide and left hand image 1/3 width of screen with text label 2/3 screen width to the right of the image. This is completely created in code. I created a View Controller on the storyboard as I needed to attach a navgation controller.
    This is a collection view of vertically scrolling cells 1/3 high as wide and left hand image 1/3 width of screen with text label 2/3 screen width to the right of the image. This is completely created in code. I created a View Controller on the storyboard as I needed to attach a navgation controller. All you ever ever need to adjust is the imageAspectRatio and te image will always be 1/3 of the width of the cell row.

    ViewController code.



    import UIKit

    let imageAspectRatio : CGFloat = 1.2

    class MyViewController: UIViewController,
    UICollectionViewDelegateFlowLayout,
    UICollectionViewDataSource {
    @@ -28,10 +30,10 @@ class MyViewController: UIViewController,

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    layout.itemSize = CGSize(width: screenWidth, height: screenWidth / 3)
    layout.itemSize = CGSize(width: screenWidth, height: (screenWidth / 3) * imageAspectRatio)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.estimatedItemSize.height = screenWidth / 3
    layout.estimatedItemSize.height = (screenWidth / 3) * imageAspectRatio
    layout.estimatedItemSize.width = screenWidth

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    @@ -107,11 +109,11 @@ class MyCell: UICollectionViewCell {

    override init(frame: CGRect) {

    imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.size.width / 3, height: frame.size.width / 3))
    imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.size.width / 3, height: (frame.size.width / 3) * imageAspectRatio))

    imageView.addBorderBottom(size: 1.0, color: .red)

    textLabel = UILabel(frame: CGRect(x: 0, y: 0, width: frame.size.width/2*3 , height: frame.size.width / 3))
    textLabel = UILabel(frame: CGRect(x: 0, y: 0, width: frame.size.width/2*3 , height: (frame.size.width / 3) * imageAspectRatio)

    textLabel.addBorderBottom(size: 1.0, color: .red)

  2. markhorrocks revised this gist Dec 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ class MyViewController: UIViewController,

    collectionView.delegate = self

    collectionView.dataSource = self // added by Mark
    collectionView.dataSource = self

    collectionView!.register(WishListCell.self, forCellWithReuseIdentifier: "Cell")

  3. markhorrocks revised this gist Dec 18, 2016. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -46,13 +46,9 @@ class MyViewController: UIViewController,

    self.view.addSubview(collectionView)

    // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
  4. markhorrocks revised this gist Dec 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -75,7 +75,7 @@ class MyViewController: UIViewController,
    }
    }

    ClolectionViewCell code:
    CollectionViewCell code:


    import UIKit
  5. markhorrocks revised this gist Dec 18, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,10 @@
    This is a collection view of vertically scrolling cells 1/3 high as wide and left hand image 1/3 width of screen with text label 2/3 screen width to the right of the image. This is completely created in code. I created a View Controller on the storyboard as I needed to attach a navgation controller.

    ViewController code.



    import UIKit
    import Alamofire

    class MyViewController: UIViewController,
    UICollectionViewDelegateFlowLayout,
  6. markhorrocks created this gist Dec 18, 2016.
    138 changes: 138 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,138 @@
    ViewController code.

    import UIKit
    import Alamofire

    class MyViewController: UIViewController,
    UICollectionViewDelegateFlowLayout,
    UICollectionViewDataSource {

    var collectionView: UICollectionView!

    var cellId = "Cell"

    var screenSize: CGRect!
    var screenWidth: CGFloat!
    var screenHeight: CGFloat!

    override func viewDidLoad() {

    super.viewDidLoad()

    screenSize = UIScreen.main.bounds
    screenWidth = screenSize.width
    screenHeight = screenSize.height

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    layout.itemSize = CGSize(width: screenWidth, height: screenWidth / 3)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.estimatedItemSize.height = screenWidth / 3
    layout.estimatedItemSize.width = screenWidth

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)

    collectionView.delegate = self

    collectionView.dataSource = self // added by Mark

    collectionView!.register(WishListCell.self, forCellWithReuseIdentifier: "Cell")

    collectionView.backgroundColor = UIColor.white

    self.view.addSubview(collectionView)

    // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 20
    }

    func collectionView(_ collectionView: UICollectionView,
    cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? MyCell

    cell!.imageView.image = UIImage(named: "star")

    cell!.textLabel.text = "Text"

    return cell!
    }
    }

    ClolectionViewCell code:


    import UIKit

    extension UIView {
    func addBorderTop(size: CGFloat, color: UIColor) {
    addBorderUtility(x: 0, y: 0, width: frame.width, height: size, color: color)
    }
    func addBorderBottom(size: CGFloat, color: UIColor) {
    addBorderUtility(x: 0, y: frame.height - size, width: frame.width, height: size, color: color)
    }
    func addBorderLeft(size: CGFloat, color: UIColor) {
    addBorderUtility(x: 0, y: 0, width: size, height: frame.height, color: color)
    }
    func addBorderRight(size: CGFloat, color: UIColor) {
    addBorderUtility(x: frame.width - size, y: 0, width: size, height: frame.height, color: color)
    }
    private func addBorderUtility(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat, color: UIColor) {
    let border = CALayer()

    border.backgroundColor = color.cgColor

    border.frame = CGRect(x: x, y: y, width: width, height: height)

    layer.addSublayer(border)
    }
    }

    class MyCell: UICollectionViewCell {

    var textLabel: UILabel
    var imageView: UIImageView

    override init(frame: CGRect) {

    imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.size.width / 3, height: frame.size.width / 3))

    imageView.addBorderBottom(size: 1.0, color: .red)

    textLabel = UILabel(frame: CGRect(x: 0, y: 0, width: frame.size.width/2*3 , height: frame.size.width / 3))

    textLabel.addBorderBottom(size: 1.0, color: .red)

    super.init(frame: frame)

    imageView.contentMode = UIViewContentMode.scaleAspectFit

    contentView.addSubview(imageView)

    textLabel.font = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize)

    textLabel.textAlignment = .center

    contentView.addSubview(textLabel)

    }

    required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
    }

    }