Skip to content

Instantly share code, notes, and snippets.

@nrlnishan
nrlnishan / DateTime.swift
Last active May 1, 2017 15:05
Common date & time operation in swift
import UIKit
// Awesome Library: SwiftDate
/* Things:
1. Compare dates
2. Break dates into different components
3. Calculate dates by adding date components
@nrlnishan
nrlnishan / ViewController-Popup.txt
Last active July 1, 2017 07:21
Showing UIViewController as popup within another UIViewController
Steps:
1. Create two view controller namely FirstViewController & SecondViewController
2. Set the background color of the default view of SecondViewController to clear color or give appropriate opacity
3. Create whatever UI You want in SecondViewController
4. Set the
self.modalPresentationStyle = .currentContext
in viewDidLoad of FirstViewController.
5. Use a modal segue or present the SecondViewController Modally.
@nrlnishan
nrlnishan / UIColorExtension.swift
Created October 7, 2017 08:04
UIColor Extensions
class func from(r:CGFloat,g:CGFloat,b:CGFloat,a:CGFloat) -> UIColor {
return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
}
// Hex should be in format #bcbcbc
class func from (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
@nrlnishan
nrlnishan / CustomUIView
Created November 1, 2017 15:28
Creating a custom UIView using Xib
Steps for creating custom UIView.
1. Create a xib file, eg: MyCustomView.xib
2. Do necessary layout in xib file
3. Create a UIView file. eg: MyCustomView.swift.
4. Set this MyCustomView.swift file as the file owner of that xib
5. Create necessary outlets from the .xib file to .swift file. Important: Create one outlet for the root parent UIView to .swift file.
Name this outlet as as containerView
6. Override both the init method i.e using decoder & frame
7. Set the code as follows
@nrlnishan
nrlnishan / MultipleEnvironments-iOS.txt
Last active January 17, 2018 16:27
Managing multiple environments in Xcode
1. Duplicate Build Configurations from Project Settings
- Example: Duplicate debug & name it as debugDev. Duplicate release & name it as releaseDev.
2. Change Project's bundle identifier from Target Build Settings
- Change it to reflect build configurations
3. Create User Defined Settings
- There is + sign on navigation bar in Build Settings. Click on it & create User Defined Settings as needed
- Example: If you need different names on different builds, add setting name APP_DISPLAY_NAME & so on.
- You need to use this created settings on info.plist file. Eg, Change Bundle Display Name value to $(APP_DISPLAY_NAME).
@nrlnishan
nrlnishan / ConstraintPriority.txt
Created February 16, 2018 05:03
Content Hugging & Compression Resistance
1. Content Hugging Property:
- This priority defines whether the content size should increase beyond its intrinsic size.
- Content Hugging :: Content doesnot want to grow.
Horizontal Hugging: Higher priority means that content width doesnot want to grow beyond its intrinsic width.
Vertical Hugging: Higher priority means that content height doesnot want to grow beyond its intrinsic height.
2. Content Compression Resistance
- This priority defines whether the content size should decrease beyond its intrinsic size.
- Content Compression Resistance :: Content doesnot want to shrink
@nrlnishan
nrlnishan / Adaptive-Scrollview1.swift
Created May 19, 2018 14:59
Scrollview - Adaptive View
// DRAFT:-
1. Give scrollview with top, leading, trailing & bottom constraint
2. Give the container view inside scrollview with
- Center x to scrollview
- Center y to scrollview
- trailing to scrollview
- bottom to scrollview
- Equal Height to scrollview
- Equal Width to scrollview
@nrlnishan
nrlnishan / shakeit.swift
Created February 14, 2019 18:39
Shake Animation
extension UIView {
func shake() {
let animation = CAKeyframeAnimation()
animation.keyPath = "position.x"
animation.values = [0, 10, -10, 10, -5, 5, -5, 0 ]
animation.keyTimes = [0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1]
animation.duration = 0.4
animation.isAdditive = true
@nrlnishan
nrlnishan / NoRetainCycle.swift
Last active March 9, 2020 04:59
Example showing no retain cycle
class SecondViewController: UIViewController {
var someText = ""
var controller: UIAlertController!
var label = UILabel()
override func loadView() {
view = UIView()
view.backgroundColor = .white
@nrlnishan
nrlnishan / property-wrapper-1.swift
Created May 19, 2020 14:47
Property Wrapper in Swift - 1
struct AppSettings {
// Update the respective value in UserDefault
static var isDarkModeEnabled = false {
didSet {
UserDefaults.standard.setValue(isDarkModeEnabled, forKey: "is_dark_mode_enabled")
}
}
}