The iOS dev cheat sheet

So I’ve been meaning to do this post for a while. I’m just going to publish a whole lot of code snippets that I’ve found really useful and most of which I use on a daily basis. I hope you find them useful:

ACCESS THE APP DELEGATE FROM ANYWHERE
this one’s especially useful for throwing up a black overlay with a spinning loader or some such thing over the top of the whole app, just add it to the app delegate’s window.view and remove when done.

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

CYCLE THROUGH ALL SUB VIEWS AND SET HIDDEN = YES
Just incase you want to switch everything off 🙂

- (void)setBackgroundToClearForView:(UIView *)view {
    NSLog(@"CYCLING THROUGH SUB VIEWS 1");
    if ([view subviews]) {
        for (UIView *subView in [view subviews]) {
            NSLog(@"CYCLING THROUGH SUB VIEWS 2");
            [self setBackgroundToClearForView:subView];
        }
    }
    
    if ([view respondsToSelector:@selector(setBackgroundColor:)]) {
        [view performSelector:@selector(setBackgroundColor:)
                   withObject:[UIColor clearColor]];
    }
}

VIEW DATA WRITTEN TO plist

 NSDictionary *Data = [[NSDictionary alloc] initWithContentsOfFile:fullPath];
    // dump the contents of the dictionary to the console
    for (id key in Data) {
        NSLog(@"bundle from file: key=%@, value=%@", key, [Data objectForKey:key]);
    }
    [Data release];


GENERATE A RANDOM NUMBER BETWEEN 0 AND 10 INCLUSIVE:

int r = arc4random() % 11;

INSERT A VIEW ON TOP:

[self.scrollView insertSubview:self.moreButton atIndex:[self.scrollView.subviews count]];

SET SOMETHING INVISIBLE, WHEN [UICOLOr CLEARCOLOR] JUST WONT WORK:

[scrollView setBackgroundColor: [UIColor colorWithRed:0 green:0 blue:0 alpha:0]];

SCALING WITHOUT POSITIONING

CGSizeMake(kWidth,kHeight));

ROTATE:

bgImageView.transform = CGAffineTransformMakeRotation(  90 * (M_PI  / 180 ));

NAV BAR STYLE

self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;

IF ELSE REGULAR EXPRESSION

([indexPath row] == 0) ? 40.0 : 40.0;

ADD A SUBVIEW

[cell.contentView insertSubview:hideSeparatorView atIndex:0];

CREATE A DYNAMIC DROP SHADOW

frame = CGRectMake(0.0, 0.0, kImageWidth, kImageHeight);
		self.mainView = [[[UIImageView alloc] initWithFrame:frame] autorelease];
		self.mainView.image = [UIImage imageNamed:@"galleryThumbBG.png"];
        
		//drop shadow
		self.layer.masksToBounds = NO;
		self.layer.cornerRadius = 0; // if you like rounded corners
		self.layer.shadowOffset = CGSizeMake(-3, 3);
		self.layer.shadowRadius = 2;
		self.layer.shadowOpacity = 0.25;

Leave a Reply

Your email address will not be published. Required fields are marked *