Skip to content

Commit

Permalink
Merge pull request #3 from bcylin/bar-button-image
Browse files Browse the repository at this point in the history
Draw bar button images with UIGraphics
  • Loading branch information
0x0c committed Dec 7, 2014
2 parents 4742790 + 3c102dd commit 2a971a0
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
3 changes: 0 additions & 3 deletions M2DWebViewController.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ Pod::Spec.new do |s|
s.requires_arc = true

s.source_files = 'Pod/Classes/**/*.{h,m}'
s.resource_bundles = {
'M2DWebViewController' => ['Pod/Assets/**']
}

# s.public_header_files = 'Pod/Classes/**/*.h'
s.frameworks = 'WebKit'
Expand Down
Empty file removed Pod/Assets/.gitkeep
Empty file.
Binary file removed Pod/Assets/M2DWebViewController_left.png
Binary file not shown.
Binary file removed Pod/Assets/M2DWebViewController_left@2x.png
Binary file not shown.
Binary file removed Pod/Assets/M2DWebViewController_right.png
Binary file not shown.
Binary file removed Pod/Assets/M2DWebViewController_right@2x.png
Binary file not shown.
53 changes: 51 additions & 2 deletions Pod/Classes/M2DWebViewController/M2DWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,55 @@

#import "M2DWebViewController.h"

static const CGSize M2DArrorIconSize = {18, 18};

typedef NS_ENUM(NSUInteger, ArrorIconDirection) {
ArrorIconDirectionLeft,
ArrorIconDirectionRight
};

@implementation UIImage (M2DArrowIcon)

+ (UIImage *)m2d_arrowIconWithDirection:(ArrorIconDirection)direction size:(CGSize)size
{
if (CGSizeEqualToSize(size, CGSizeZero)) {
return [[UIImage alloc] init];
}

UIGraphicsBeginImageContextWithOptions(size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = {CGPointZero, size};

CGContextSaveGState(context);
CGContextBeginPath(context);

if (direction == ArrorIconDirectionRight) {
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMidY(rect));
CGContextAddLineToPoint(context, 0, CGRectGetMaxY(rect));
} else {
CGContextMoveToPoint(context, CGRectGetMaxX(rect), 0);
CGContextAddLineToPoint(context, 0, CGRectGetMidY(rect));
CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
}

CGContextClosePath(context);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextFillPath(context);
CGContextRestoreGState(context);

UIImage *icon = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return icon;
}

@end


////////////////////////////////////////////////////////////////////////////////


@interface M2DWebViewController ()

@end
Expand Down Expand Up @@ -68,8 +117,8 @@ - (void)viewWillAppear:(BOOL)animated
[self.navigationController setToolbarHidden:NO animated:YES];
if (goBackButton_ == nil) {
NSArray *toolbarItems = nil;
goBackButton_ = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithContentsOfFile:[self getFilePath:@"M2DWebViewController_left.png"]] style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
goForwardButton_ = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithContentsOfFile:[self getFilePath:@"M2DWebViewController_right.png"]] style:UIBarButtonItemStylePlain target:self action:@selector(goForward:)];
goBackButton_ = [[UIBarButtonItem alloc] initWithImage:[UIImage m2d_arrowIconWithDirection:ArrorIconDirectionLeft size:M2DArrorIconSize] style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
goForwardButton_ = [[UIBarButtonItem alloc] initWithImage:[UIImage m2d_arrowIconWithDirection:ArrorIconDirectionRight size:M2DArrorIconSize] style:UIBarButtonItemStylePlain target:self action:@selector(goForward:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *fixedSpace19 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedSpace19.width = 19;
Expand Down

0 comments on commit 2a971a0

Please sign in to comment.