Skip to content

Instantly share code, notes, and snippets.

@lucaspang
Created January 8, 2016 02:16
Show Gist options
  • Save lucaspang/b94454aa164d871baa0f to your computer and use it in GitHub Desktop.
Save lucaspang/b94454aa164d871baa0f to your computer and use it in GitHub Desktop.
iOS UINavigationBar hide/show hairline
@interface UINavigationBar (Addition)
- (void)hideBottomHairline;
- (void)showBottomHairline;
@end
@implementation UINavigationBar (Addition)
/**
* Hide 1px hairline of the nav bar
*/
- (void)hideBottomHairline {
UIImageView *navBarHairlineImageView = [self findHairlineImageViewUnder:self];
navBarHairlineImageView.hidden = YES;
}
/**
* Show 1px hairline of the nav bar
*/
- (void)showBottomHairline {
// Show 1px hairline of translucent nav bar
UIImageView *navBarHairlineImageView = [self findHairlineImageViewUnder:self];
navBarHairlineImageView.hidden = NO;
}
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}
- (void)makeTransparent {
[self setTranslucent:YES];
[self setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
self.backgroundColor = [UIColor clearColor];
self.shadowImage = [UIImage new]; // Hides the hairline
[self hideBottomHairline];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment