Skip to content

Instantly share code, notes, and snippets.

View abhi21git's full-sized avatar
:octocat:

Abhishek Maurya abhi21git

:octocat:
View GitHub Profile
@abhi21git
abhi21git / GradientView.swift
Created November 1, 2022 07:52
Create gradients and cache them to reduce memory footprint.
class GradientView: UIImageView {
var gradientColors: [CGColor] = [UIColor.greyishBrown.withAlphaComponent(0.2).cgColor, UIColor.greyishBrown.cgColor]
var locations: [CGFloat]? = [0.0, 1.0]
override func layoutSubviews() {
self.contentMode = .scaleToFill
self.backgroundColor = .hotPink
self.image = drawGradientColor(in: self.bounds, colors: gradientColors)
}
@abhi21git
abhi21git / CollectionCellSize.swift
Last active November 1, 2022 07:54
Dynamic cell size based on screenWidth
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let totalAvailableWidth = collectionView.bounds.width
let padding: CGFloat = 25
let interSpace: CGFloat = 15
let numOfCellInaRow: CGFloat = 3
let cellWidth: CGFloat = (totalAvailableWidth - (padding * 2) - (interSpace * (numOfCellInaRow - 1))) / numOfCellInaRow
/// padding * 2 = leading + trailing space, (interSpace * (numOfCellInaRow - 1)) = totalInterspacing as 3 cells will have 2 interspace
let cellSize = CGSize(width: cellWidth, height: cellWidth)
return cellSize
}
import UIKit
// Confirm this to your cell/header if using Prototype cell
public protocol ReusableView: AnyObject {
static var defaultReuseIdentifier: String { get }
}
extension ReusableView where Self: UIView {
public static var defaultReuseIdentifier: String {
return String(describing: self)
@abhi21git
abhi21git / ExtensionURLRequest.swift
Last active April 8, 2025 10:51
Swift cURL Printer
//
// ExtensionURLRequest.swift
//
// Created by Abhishek Maurya on 16/07/20.
// Copyright © 2020. All rights reserved.
//
import Foundation
extension URLRequest {