Skip to content

Instantly share code, notes, and snippets.

View jimbrayrcp's full-sized avatar
😀

Jim Bray jimbrayrcp

😀
  • San Antonio, Texas
View GitHub Profile
@jimbrayrcp
jimbrayrcp / Notification - Observer.swift
Last active May 9, 2020 13:51
Swift 5: iOS: 13.1 Runs a script in another viewController: in this gists example, you can reload data in a TableView from a different ViewController
// --------------- VIEW CONTROLLER 1 -----------------------
// MARK: NOTIFICATION
extension Notification.Name {
static let reload = Notification.Name("reload")
}
// MARK: CALLER
NotificationCenter.default.post(name: .reload, object: nil)
// --------------- VIEW CONTROLLER 2 -----------------------
@jimbrayrcp
jimbrayrcp / TableView Separater Height.swift
Created May 7, 2020 22:39
Increases the separator size between cells by adding a border. Also, corner radius can be removed or added if desired.
let tableCellBGClight: UIColor = systemBlue
// ****** cellForRowAt ******
cell.layer.masksToBounds = true
cell.layer.cornerRadius = 10
cell.layer.borderWidth = 2
cell.layer.shadowOffset = CGSize(width: -1, height: 1)
@jimbrayrcp
jimbrayrcp / Swipe Trailing (Left Swipe).swift
Created May 6, 2020 16:19
Swift 5: iOS: 13.1 - tableView Swipe Left Delete Action (or any function you want to add) Uses system images
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .normal, title: "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
// *** add the object you want to delete here ***
// object.delete
success(true)
})
deleteAction.backgroundColor = .red
deleteAction.image = UIImage(systemName: "trash.fill")
deleteAction.backgroundColor = .green
@jimbrayrcp
jimbrayrcp / Alert .alert.swift
Created May 6, 2020 16:10
Swift 5: iOS: 13.1 Insert into any method for an alert
let alertTitle = "Test"
let alertMessage = "Test Message"
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { action in
// Add Actions to this closure
print("Test Response")
}))
alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
self.present(alert, animated: true)
@jimbrayrcp
jimbrayrcp / Alert .actionSheet.swift
Created May 6, 2020 16:04
Swift 5: iOS: 13.1 Insert into any method for an action sheet alert type
let alertTitle = "Test"
let alertMessage = "Test Message"
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { action in
// Add Actions to this closure
print("Test Response")
}))
alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
self.present(alert, animated: true)
@jimbrayrcp
jimbrayrcp / Refresh UITableViewController.swift
Last active May 6, 2020 16:05
pull to refresh UITableVIewController Swift 5: iOS: 13.1
// -------------------------------------------
// Settings: Select the table view controller
// open the attributes inspector, and enable
// refreshing
// Add title color and center
// ****************************
// ADD TO VIEW DID LOAD
@jimbrayrcp
jimbrayrcp / Swipe Leading (Right Swipe).swift
Last active May 6, 2020 16:21
Swift 5: iOS: 13.1 - tableView Swipe Right Delete (or any function you want to add) Uses system images
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let action = UIContextualAction(style: .normal, title: "", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
// *** add object To be deleted ***
// object.delete
success(true)
})
action.image = UIImage(systemName: "trash.fill")
action.backgroundColor = .green
return UISwipeActionsConfiguration(actions: [action])
}
@jimbrayrcp
jimbrayrcp / Keyboard Dismiss.swift
Last active May 6, 2020 16:06
Works in Swift 5: iOS: 13.1 Dismiss a keyboard when tapping outside of field
// ************************************
// ADD TO VIEW DID LOAD
// ------------------------------------
let tap: UITapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(UIInputViewController.dismissKeyboard))
//Uncomment to prevent interference of cancel button
//tap.cancelsTouchesInView = false
@jimbrayrcp
jimbrayrcp / Date Formatter (extension).swift
Last active May 6, 2020 16:06
Swift 5: iOS: 13.1 Date formatter and converter: Converts: dateString to Date dateString to Formatted Date String date to Formatted date string
import Foundation
// USE:
var dateString = "14.05.2020T10:30:10"
let dateFormat = DateFormatter(format: "dd.MM.yyyy'T'HH:mm:ss")
let dateFormat1 = DateFormatter(format: "MM/dd/yyyy '@' HH:mm")
let date = Date()
print("date String -> toDate: \(dateString.toDate(dateFormatter: dateFormat)!)")
print("date String -> toDateString: \(dateString.toDateString(dateFormatter: dateFormat, outputFormat: "MM/dd/yy")!)")
@jimbrayrcp
jimbrayrcp / String to Integer (extension).swift
Last active May 6, 2020 16:07
Swift 5: iOS: 13.1 Converts String to Integer, Double, Float, Bool Use : let myString = "51.55522222222" print(myString.toInteger() ?? "") print(myString.toDouble() ?? "") print(myString.toFloat() ?? "") print(myString.toBool() ?? "")
import Foundation
// ...
extension String {
// Converts String to Integer
public func toInteger() -> Int? {
if let num = NumberFormatter().number(from: self) {
return num.intValue
} else {
return nil
}