Created
January 8, 2016 02:16
-
-
Save lucaspang/b94454aa164d871baa0f to your computer and use it in GitHub Desktop.
iOS UINavigationBar hide/show hairline
This file contains 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
@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