Last active
July 6, 2023 19:17
-
-
Save markhorrocks/0c366f06cdad8ef92871e2591bf8178d to your computer and use it in GitHub Desktop.
Swift 3 iOS dynamic layout CollectionView cell all in code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | |
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) * imageAspectRatio) | |
layout.minimumInteritemSpacing = 0 | |
layout.minimumLineSpacing = 0 | |
layout.estimatedItemSize.height = (screenWidth / 3) * imageAspectRatio | |
layout.estimatedItemSize.width = screenWidth | |
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) | |
collectionView.delegate = self | |
collectionView.dataSource = self | |
collectionView!.register(WishListCell.self, forCellWithReuseIdentifier: "Cell") | |
collectionView.backgroundColor = UIColor.white | |
self.view.addSubview(collectionView) | |
} | |
} | |
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! | |
} | |
} | |
CollectionViewCell 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) * 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) * imageAspectRatio) | |
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") | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment