Skip to content

Instantly share code, notes, and snippets.

View TheCodedSelf's full-sized avatar

Keegan Rush TheCodedSelf

View GitHub Profile
@TheCodedSelf
TheCodedSelf / VisibleHeight.m
Created January 17, 2016 10:38
iOS: Get the visible height of a table view (minus the tab bar and navigation bar
CGFloat height = self.tableView.bounds.size.height - [UIApplication sharedApplication].statusBarFrame.size.height;
height = height - self.navigationController.navigationBar.frame.size.height;
height = height - self.tabBarController.tabBar.bounds.size.height;
@TheCodedSelf
TheCodedSelf / NavigationBars.swift
Last active January 19, 2016 17:52
Playing with UINavigationBars
// Set title text
self.navigationItem.title = "Title"
// Set title text color
let desiredColor = UIColor.whiteColor()
navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : desiredColor]
// Set back bar button item title
let backButton = UIBarButtonItem(title: "Back", style: .Plain, target: nil, action: nil)
self.navigationItem.backBarButtonItem = backButton
@TheCodedSelf
TheCodedSelf / Misc.swift
Last active January 23, 2016 10:22
Miscellaneous
// Random number between 0 and n
let n = 42
let randomNumber = Int(arc4random_uniform(n))
// Instantiate a view controller that exists in a storyboard
let storyboardName = "Main"
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let storyboardIdForViewController = "MyVC" // Specified in IB on the specific view controller instance
let myVc: MyViewController = storyboard.instantiateViewControllerWithIdentifier(storyboardIdForViewController) as! MyViewController
import UIKit
extension UIView
{
func shake()
{
let shakeAngle = (5 * M_PI) / 360
let shakeRotation = CAKeyframeAnimation(keyPath: "transform.rotation")
@TheCodedSelf
TheCodedSelf / xopen
Last active September 8, 2016 05:19
Bash script that opens any xcworkspace or xcodeproj found in the current directory in Xcode
#!/bin/bash
#
# Opens any xcworkspace or xcodeproj found in the current directory in Xcode.
#
openfile () (
file="$1"
echo "Opening $file";
open $file;
)
@TheCodedSelf
TheCodedSelf / Disable Alert Button.swift
Last active March 31, 2024 18:00
Disable Alert Controller button if Alert Controller text field is empty or whitespace
import UIKit
// Create an alert controller
let alertController = UIAlertController(title: "Alert", message: "Please enter text", preferredStyle: .alert)
// Create an OK Button
let okAction = UIAlertAction(title: "OK", style: .default) { (_) in
// Print "OK Tapped" to the screen when the user taps OK
print("OK Tapped")
}
@TheCodedSelf
TheCodedSelf / MainTryCatch.m
Last active October 18, 2016 09:00
Wrap Objective-C Main method in a try-catch block for exception handling
int main(int argc, char *argv[]) {
@autoreleasepool {
@try {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
} @catch (NSException *exception) {
#if DEBUG
NSLog(@"\n\n********************");
NSLog(@"UNHANDLED EXCEPTION:");
NSLog(@"%@", exception);
NSLog(@"Stack trace: %@", [exception callStackSymbols]);
@TheCodedSelf
TheCodedSelf / author-amend.sh
Created February 12, 2017 17:19
Change all commits with author email "bruce.wayne@wayneenterprises.com" to "batman@justiceleague.com"
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="bruce.wayne@wayneenterprises.com"
CORRECT_NAME="The Dark Knight"
CORRECT_EMAIL="batman@justiceleague.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
@TheCodedSelf
TheCodedSelf / ImageStore.swift
Created April 29, 2017 06:05
Store, retrieve, and delete images in your iOS app's local storage
import UIKit
struct ImageStore {
static func delete(imageNamed name: String) {
guard let imagePath = ImageStore.path(for: name) else {
return
}
try? FileManager.default.removeItem(at: imagePath)
@TheCodedSelf
TheCodedSelf / RxSwiftChainObservablesExample.swift
Last active June 29, 2018 19:36
RxSwift: An example of causing one observable to rely on the output of another
import RxSwift
func submit(number: Int) {
let validateAndPerformServiceCallIfSuccessful = performValidation(numberThatShouldBeEven: number)
.flatMap { () -> Observable<Void> in
print("Validated successfully")
return performServiceCall() // For any .next events from performValidation, perform the service call
}
// By subscribing, validation is performed and the service call is executed for any .next events from performValidation