Skip to content

Instantly share code, notes, and snippets.

@rodydavis
rodydavis / UIColor.swift
Created November 7, 2017 15:29
Making new UIColors easier
//Extension to make it easy for UIColor init
extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
let newRed = CGFloat(red)/255
let newGreen = CGFloat(green)/255
let newBlue = CGFloat(blue)/255
self.init(red: newRed, green: newGreen, blue: newBlue, alpha: 1.0)
}
}
@rodydavis
rodydavis / UITableViewCell.swift
Last active November 7, 2017 15:56
Table View Cell button actions and actions from image view
class DashboardCell: UITableViewCell {
// MARK: - Properties
static let reuseIdentifier = "DashboardCell"
// MARK: -
@IBOutlet weak var lblHeaderTitle: UILabel!
//@IBOutlet weak var imgContactDetails: UIImageView!
@IBOutlet weak var lblDetail1: UILabel!
func map(address: String, title: String, name: String) {
let location = address
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(location) { [weak self] placemarks, error in
if let placemark = placemarks?.first, let location = placemark.location {
let mark = MKPlacemark(placemark: placemark)
let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
if var region = self?.mapView.region {
region.center = location.coordinate
@rodydavis
rodydavis / MapKitView.swift
Last active November 7, 2017 16:11
View Controller for Map Kit (Get location from string)
//
// ContactMapView.swift
import UIKit
import MapKit
import Contacts
import CoreLocation
var addressString = "" //Pass Address String from other View Controllers
@rodydavis
rodydavis / UITableviewController.swift
Last active November 9, 2017 12:47
Call, Email and Delete Cell Actions (Left and Right Swipe)
override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
let indexPathIndex = tableView.indexPathForSelectedRow
let indexPath = indexPathIndex?.row ?? 0
let callButton = UITableViewRowAction(style: .normal, title: "📞\n Call") { action, index in
print("call button tapped")
let number = data[indexPath].phone ?? ""
print("Number: " + number)
UIApplication.makeAPhoneCall(number: number.replacingOccurrences(of: "-", with: ""))
@rodydavis
rodydavis / MapKitMinimal.swift
Last active November 9, 2017 12:49
Address From String Function
import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
var restaurant: RestaurantMO!
override func viewDidLoad() {
@rodydavis
rodydavis / UITextField.swift
Created November 15, 2017 15:13
Scroll the Background so that the text fields are always visible (Swift 4)
/*
https://github.com/hackiftekhar/IQKeyboardManager
Extremely easy to install Swift or Objective-C.
Here how it works:
IQKeyboardManager (Swift):- IQKeyboardManagerSwift is available through CocoaPods, to install it simply add the following line to your Podfile: (#236)
*/
// pod 'IQKeyboardManagerSwift'
//In AppDelegate.swift, just import IQKeyboardManagerSwift framework and enable IQKeyboardManager.
@rodydavis
rodydavis / States.swift
Created November 20, 2017 19:56
States Swift 4
//States
let states = [ "AK","AL","AR","AS","AZ","CA","CO","CT","DC","DE","FL","GA","GU","HI","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY","OH","OK","OR","PA","PR","RI","SC","SD","TN","TX","UT","VA","VI","VT","WA","WI","WV","WY"]
import ContactsUI
import UIKit
class ViewController: UIViewController, CNContactPickerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
openAddressbook(vc: ViewController())
}
@rodydavis
rodydavis / CoreDataOrderSave.swift
Created January 14, 2018 02:11
How to save core data order list after rearranging and support for CRUD operations
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
if let appDel = (UIApplication.shared.delegate as? AppDelegate) {
let contxt = appDel.persistentContainer.viewContext
RefreshCoredata()
if fromIndexPath.row > to.row
{
for i in to.row..<fromIndexPath.row
{
songs[i].setValue(i+1, forKey: "orderPosition")