Skip to content

Instantly share code, notes, and snippets.

View MaherKSantina's full-sized avatar
🎯
Focusing

Maher Santina MaherKSantina

🎯
Focusing
  • New South Wales, Australia
View GitHub Profile
@MaherKSantina
MaherKSantina / UIView+Embed.swift
Last active October 22, 2019 06:18
This gist helps with placing views inside each other using autolayout constraints
public typealias ConstraintsConfiguration = (_ top: NSLayoutConstraint, _ left: NSLayoutConstraint, _ bottom: NSLayoutConstraint, _ right: NSLayoutConstraint) -> Void
extension UIView {
/**
Embeds a subview inside the current view and adds constraints to fit the subview in the whole view. An optional constraints configuration closure can be specified to do extra customization for the added constraints, or you can keep a reference for the added constraints in this closure.
- Parameter subview: The subview that will be added
- Parameter constraintsConfiguration: The constraint configuration that will be applied to the newly added constraints
*/
public func addSubviewWithConstraints(_ subview: UIView, constraintsConfiguration: ConstraintsConfiguration? = nil) {
// Required to disable auto generation of constraints
@MaherKSantina
MaherKSantina / Optional+Unwrap.swift
Created September 5, 2019 09:19
A way to easily safely unwrap values
extension Optional {
/**
Unwraps a value or returns the default value provided if it's nil
- parameter defaultValue: The default value that will be returned if the original value is nil
*/
func unwrap(defaultValue: Wrapped) -> Wrapped {
guard let self = self else {
// Make the app crash in debug but not in production
@MaherKSantina
MaherKSantina / pod_snippet.swift
Created September 26, 2018 00:25
Change the version number of dependencies in a pod
post_install do |installer|
installer.pods_project.targets.each do |target|
if ['RxSwift', 'RxCocoa', 'RxAlamofire', 'RxTest', 'RxBlocking'].include? target.name
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '3.3'
end
end
end
end
class ListingView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
initView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initView()
// Step 1
guard let xibItems = Bundle.main.loadNibNamed("ListingView", owner: self, options: nil), let firstXibView = xibItems[0] as? UIView else {
fatalError("Xib is empty or first view is not a UIView")
}
// Step 2
firstXibView.translatesAutoresizingMaskIntoConstraints = false
// Step 3
let viewConstraints = [NSLayoutAttribute.top, NSLayoutAttribute.left, NSLayoutAttribute.bottom, NSLayoutAttribute.right].map { (attribute) -> NSLayoutConstraint in
return NSLayoutConstraint(item: firstXibView, attribute: attribute, relatedBy: .equal, toItem: self, attribute: attribute, multiplier: 1, constant: 0)
class ListingView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
initView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initView()
import UIKit
import MSAutoView
class ListingView: MSAutoView { }
@MaherKSantina
MaherKSantina / snippet.swift
Created July 24, 2018 08:33
MSPCVDI Snippet 0
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return cellSpacing
}
@MaherKSantina
MaherKSantina / snippet.swift
Created July 24, 2018 08:28
MSPCVDI Snippet 5
public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let target = targetContentOffset.pointee
//Current scroll distance is the distance between where the user tapped and the destination for the scrolling (If the velocity is high, this might be of big magnitude)
let currentScrollDistance = target.x - currentScrollOffset.x
//Make the value an integer between -1 and 1 (Because we don't want to scroll more than one item at a time)
let coefficent = Int(max(-1, min(currentScrollDistance/scrollThreshold, 1)))
let currentIndex = Int(round(currentScrollOffset.x/itemWidth))
let adjacentItemIndex = currentIndex + coefficent
let adjacentItemIndexFloat = CGFloat(adjacentItemIndex)
let adjacentItemOffsetX = adjacentItemIndexFloat * (itemWidth(scrollView) + cellSpacing)
@MaherKSantina
MaherKSantina / snippet.swift
Created July 24, 2018 08:26
MSPCVDI Snippet 4
public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
currentScrollOffset = scrollView.contentOffset
}