dingyi
3/7/2012 - 5:48 PM

How to write a pixel perfect backbutton in code. (stuff goes into viewDidLoad). Taken from http://pspdfkit.com

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:)];)