Created
May 4, 2015 20:38
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface NAHMainTable : UITableViewController | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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