Skip to content

Instantly share code, notes, and snippets.

@florianbachmann
Created March 7, 2012 17:55
Show Gist options
  • Save florianbachmann/1994696 to your computer and use it in GitHub Desktop.
Save florianbachmann/1994696 to your computer and use it in GitHub Desktop.
How to write a pixel perfect backbutton in code. (stuff goes into viewDidLoad). Taken from http://pspdfkit.com
forwardBarButtonItem_ = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(goForward:)];
// back button needs more treatment...
UIGraphicsBeginImageContextWithOptions(CGSizeMake(27, 22), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents([UIColor blackColor].CGColor));
CGContextBeginPath(context);
CGContextMoveToPoint(context, 8.0f, 13.0f);
CGContextAddLineToPoint(context, 24.0f, 4.0f);
CGContextAddLineToPoint(context, 24.0f, 22.0f);
CGContextClosePath(context);
CGContextFillPath(context);
UIImage *backImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
PSPDF_IF_IOS5_OR_GREATER(
UIGraphicsBeginImageContextWithOptions(CGSizeMake(20, 20), NO, 0.0);
context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents([UIColor blackColor].CGColor));
CGContextBeginPath(context);
CGContextMoveToPoint(context, 8.0f, 13.0f);
CGContextAddLineToPoint(context, 20.0f, 6.0f);
CGContextAddLineToPoint(context, 20.0f, 20.0f);
CGContextClosePath(context);
CGContextFillPath(context);
UIImage *smallBackImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
backBarButtonItem_ = [[UIBarButtonItem alloc] initWithImage:backImage landscapeImagePhone:smallBackImage style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
)
PSPDF_IF_PRE_IOS5(backBarButtonItem_ = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment