Skip to content

Instantly share code, notes, and snippets.

@mnem
Created May 4, 2015 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnem/6cbe03350cd2eba47ba8 to your computer and use it in GitHub Desktop.
Save mnem/6cbe03350cd2eba47ba8 to your computer and use it in GitHub Desktop.
An opaque navigation bar with an arbitrary background color and single pixel divider color on iOS 7+. This is more faff than it should be.
#import <UIKit/UIKit.h>
@interface NAHMainTable : UITableViewController
@end
#import "NAHMainTable.h"
@interface NAHMainTable ()
@end
@implementation NAHMainTable
- (void)viewDidLoad {
[super viewDidLoad];
[self hairlineNavigationBarLineWithFillColor:[UIColor whiteColor] strokeColor:[UIColor blueColor]];
}
- (void)hairlineNavigationBarLineWithFillColor:(UIColor *)fill
strokeColor:(UIColor *)stroke {
const CGRect slice = CGRectMake(0.0f, 0.0f, 1.0f, 128.0f);
// Assuming that all colors are opaque. Paramaterise this if need be.
const BOOL opaque = YES;
// Create a new context to turn into the background image.
UIGraphicsBeginImageContextWithOptions(slice.size, opaque, [UIScreen mainScreen].scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[fill setFill];
CGContextAddRect(ctx, slice);
CGContextFillPath(ctx);
[stroke setStroke];
CGContextMoveToPoint(ctx, 0.0f, slice.size.height);
CGContextAddLineToPoint(ctx, slice.size.width, slice.size.height);
CGContextStrokePath(ctx);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// Set the background image
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
// Set an empty image for the shadow image, otherwise we'll get a double line
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 0;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment