Skip to content

Instantly share code, notes, and snippets.

View iAmrSalman's full-sized avatar

Amr Salman iAmrSalman

View GitHub Profile
@iAmrSalman
iAmrSalman / Sharable.swift
Last active April 7, 2018 19:36
[Sharable] a protocol to handle sharing via UIActivityViewController #swift #protocol
import UIKit
protocol Sharable {
func share(_ str: String, source: UIView?, completionHandler: ((_ completed: Bool) -> Void)?)
}
extension Sharable where Self: UIViewController {
func share(_ str: String, source: UIView? = nil, completionHandler: ((_ completed: Bool) -> Void)? = nil) {
let activityController = UIActivityViewController.init(activityItems: [str], applicationActivities: nil)
activityController.popoverPresentationController?.sourceView = source ?? self.view
@iAmrSalman
iAmrSalman / setupCollectionView.swift
Last active January 16, 2018 08:09
[setup collectionView] #uicollectionview #swift3
private func setupCollectionView() {
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UINib.init(nibName: "NIB_NAME", bundle: nil), forCellWithReuseIdentifier: "cell")
setupCellSizing()
}
private func setupCellSizing() {
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
@iAmrSalman
iAmrSalman / refreshControl.swift
Created January 2, 2018 10:34
[Add Pull-to-Refresh] #swift #uitableview
// Configure Refresh Control
refreshControl.addTarget(self, action: #selector(refresh(_:)), for: .valueChanged)
refreshControl.tintColor = UIColor.Kotobi.red
if #available(iOS 10.0, *) {
self.tableView.refreshControl = refreshControl
} else {
self.tableView.addSubview(refreshControl)
}
@iAmrSalman
iAmrSalman / loopable.swift
Created December 12, 2017 12:15
[Loopable] #protocol #swift
protocol Loopable {
var allProperties: [String: Any] { get }
}
extension Loopable {
var allProperties: [String: Any] {
var result = [String: Any]()
Mirror(reflecting: self).children.forEach { child in
if let property = child.label {
result[property] = child.value
}
@iAmrSalman
iAmrSalman / UIColor+FlatColors.swift
Created November 9, 2017 10:37
[flat colors] #uicolor #extension
extension UIColor {
struct FlatColor {
struct Green {
static let Fern = UIColor(netHex: 0x6ABB72)
static let MountainMeadow = UIColor(netHex: 0x3ABB9D)
static let ChateauGreen = UIColor(netHex: 0x4DA664)
static let PersianGreen = UIColor(netHex: 0x2CA786)
}
struct Blue {
@iAmrSalman
iAmrSalman / UIColorExtension.swift
Created November 9, 2017 08:21
[color from hex] #uicolor #extension
extension UIColor {
convenience init(red: Int, green: Int, blue: Int, alpha: CGFloat) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: alpha)
}
convenience init(netHex:Int, alpha: CGFloat = 1.0) {
self.init(red:(netHex >> 16) & 0xff, green:(netHex >> 8) & 0xff, blue:netHex & 0xff, alpha: alpha)
@iAmrSalman
iAmrSalman / UIViewControllerExtension.swift
Created November 9, 2017 07:49
[viewController from Storyboard] #uiviewcontroller #swift3 #extension
import UIKit
extension UIViewController {
func viewController(withStoryBoardname storyBoardName : String) -> UIViewController? {
let storyboard = UIStoryboard(name: storyBoardName, bundle: .main)
let controller = storyboard.instantiateInitialViewController()
return controller
}
@iAmrSalman
iAmrSalman / UIColorExtension.swift
Last active November 9, 2017 07:47
[color from hex] #uicolor #extension
import UIKit
extension UIColor {
func colorFromHexString (_ hex:String, alpha: Float = 1.0) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
@iAmrSalman
iAmrSalman / gitignoreAfterPush.md
Created November 8, 2017 14:03
[remove ignored files after push] #git

The series of commands below will remove all of the items from the Git Index (not from the working directory or local repo), and then updates the Git Index, while respecting git ignores. PS. Index = Cache

First:

git rm -r --cached . 
git add . 

Then:

@iAmrSalman
iAmrSalman / BundleExtension.swift
Created November 8, 2017 08:52
[App Version & Build number] get the current build information from info.plist #swift3 #info.plist
extension Bundle {
var releaseVersionNumber: String? {
return infoDictionary?["CFBundleShortVersionString"] as? String
}
var buildVersionNumber: String? {
return infoDictionary?["CFBundleVersion"] as? String
}
}
Bundle.main.releaseVersionNumber