Last active
July 6, 2023 19:17
-
-
Save markhorrocks/0c366f06cdad8ef92871e2591bf8178d to your computer and use it in GitHub Desktop.
Revisions
-
markhorrocks revised this gist
Dec 19, 2016 . 1 changed file with 7 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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. 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) * 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) @@ -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) * 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) -
markhorrocks revised this gist
Dec 18, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -38,7 +38,7 @@ class MyViewController: UIViewController, collectionView.delegate = self collectionView.dataSource = self collectionView!.register(WishListCell.self, forCellWithReuseIdentifier: "Cell") -
markhorrocks revised this gist
Dec 18, 2016 . 1 changed file with 1 addition and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -46,13 +46,9 @@ class MyViewController: UIViewController, self.view.addSubview(collectionView) } } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 -
markhorrocks revised this gist
Dec 18, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -75,7 +75,7 @@ class MyViewController: UIViewController, } } CollectionViewCell code: import UIKit -
markhorrocks revised this gist
Dec 18, 2016 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 class MyViewController: UIViewController, UICollectionViewDelegateFlowLayout, -
markhorrocks created this gist
Dec 18, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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") } }