Skip to content

Instantly share code, notes, and snippets.

View pietrorea's full-sized avatar
✌️

Pietro Rea pietrorea

✌️
View GitHub Profile
@pietrorea
pietrorea / gist:4636954
Created January 25, 2013 19:08
How to move a UIView above the keyboard when the keyboard comes on screen. The method keyboardWillShow handles the notification UIKeyboardWillShowNotification.
- (void)keyboardWillShow:(NSNotification *)notification {
CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect convertedFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];
CGFloat animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animationCurve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:animationDuration delay:0 options:animationCurve animations:^{
self.viewToMove.bottom = self.view.height - convertedFrame.size.height - 15;
@pietrorea
pietrorea / TPSpringyModalTransition.h
Created May 15, 2014 12:46
TPSpringyModalTransition - modal transition that bounces off the top like a spring
// TPSpringyModalTransition.h
// iPeru
//
// Created by Pietro Rea on 5/4/14.
// Copyright (c) 2014 Pietro Rea. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface TPSpringyModalTransition : NSObject <UIViewControllerAnimatedTransitioning>
@pietrorea
pietrorea / CoreDataManager.swift
Created June 16, 2014 06:49
Simple Core Data stack
import CoreData
class CoreDataManager {
var context:NSManagedObjectContext
var psc:NSPersistentStoreCoordinator
var model:NSManagedObjectModel
var store:NSPersistentStore?
init() {
@pietrorea
pietrorea / UIAlertController.swift
Last active November 27, 2016 19:04
UIAlertController example
var alert = UIAlertController(title: "Title", message: "Alert view message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Save", style: .Default, handler: { (action: UIAlertAction!) in
println("Saved")
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in
println("Cancel")
}))
presentViewController(alert, animated: true, completion: nil)
@pietrorea
pietrorea / gist:c9431bcf3e9ce33140db
Last active April 19, 2017 01:44
Autolayout: the equivalent of [subview setFrame:self.view.bounds];
UIView *autoLayoutView = [[UIView alloc] init];
autoLayoutView.backgroundColor = [UIColor redColor];
autoLayoutView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:autoLayoutView];
NSDictionary *views = NSDictionaryOfVariableBindings(autoLayoutView);
NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[autoLayoutView]|"
options:nil
metrics:nil
@pietrorea
pietrorea / gist:15a0697f2aaf6d682ac6
Created July 30, 2014 00:48
Convert RFC 3339 formatted date string to NSDate
//From https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
- (NSDate *)dateFromRFC3339String:(NSString *)dateString {
static NSDateFormatter *sRFC3339DateFormatter = nil;
if (sRFC3339DateFormatter == nil) {
sRFC3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[sRFC3339DateFormatter setLocale:enUSPOSIXLocale];
@pietrorea
pietrorea / gist:089e308b4e9b46ded3ae
Last active August 29, 2015 14:05
Auto Layout ambiguous constraints debugging tool
//In UIViewController subclass
@interface UIWindow (AutoLayoutDebug)
+ (UIWindow *)keyWindow;
- (NSString *)_autolayoutTrace;
@end
- (void)didRotateFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
@pietrorea
pietrorea / gist:fbd09d5496bd018f6354
Created August 31, 2014 14:07
debugLog preprocessor macro
//Add to Project-Prefix.pch
#ifdef DEBUG
#define debugLog(...) NSLog(__VA_ARGS__)
#else
#define debugLog(...) // Nothing
#endif
NSString *html = @"<bold>Wow!</bold> Now <em>iOS</em> can create <h3>NSAttributedString</h3> from HTMLs!";
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:nil];
@pietrorea
pietrorea / gist:9887ce31f1d09c87114b
Last active August 29, 2015 14:09
Weak reference to self within a Swift block
let dataTask = urlSession.dataTaskWithURL(url!, completionHandler: { [unowned self] (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
//add code here
})