From fd2be1db028323b42cdfca9896a9c565f8689317 Mon Sep 17 00:00:00 2001 From: Jeason Date: Fri, 15 Sep 2017 15:03:21 +0800 Subject: [PATCH 1/2] fix iPhoneX lt_setBackgroundColor OverlayHeight BUG; --- LTNavigationBar/UINavigationBar+Awesome.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LTNavigationBar/UINavigationBar+Awesome.m b/LTNavigationBar/UINavigationBar+Awesome.m index 10adb5b..8dcf2c4 100644 --- a/LTNavigationBar/UINavigationBar+Awesome.m +++ b/LTNavigationBar/UINavigationBar+Awesome.m @@ -28,7 +28,8 @@ - (void)lt_setBackgroundColor:(UIColor *)backgroundColor { if (!self.overlay) { [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; - self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + 20)]; + CGFloat statusBarHeight = CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]); + self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + statusBarHeight)]; self.overlay.userInteractionEnabled = NO; self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth; // Should not set `UIViewAutoresizingFlexibleHeight` [[self.subviews firstObject] insertSubview:self.overlay atIndex:0]; From 669fabcb9df9558c4331e02ff9d0968a69f85ce9 Mon Sep 17 00:00:00 2001 From: Jeason Lee Date: Thu, 20 Sep 2018 12:08:01 +0800 Subject: [PATCH 2/2] fix iOS12 lt_setBackgroundColor bug --- LTNavigationBar/UINavigationBar+Awesome.m | 25 +++++++++++++---------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/LTNavigationBar/UINavigationBar+Awesome.m b/LTNavigationBar/UINavigationBar+Awesome.m index 8dcf2c4..57aad86 100644 --- a/LTNavigationBar/UINavigationBar+Awesome.m +++ b/LTNavigationBar/UINavigationBar+Awesome.m @@ -14,12 +14,12 @@ @implementation UINavigationBar (Awesome) static char overlayKey; -- (UIView *)overlay +- (UIImage *)overlay { return objc_getAssociatedObject(self, &overlayKey); } -- (void)setOverlay:(UIView *)overlay +- (void)setOverlay:(UIImage *)overlay { objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @@ -27,14 +27,18 @@ - (void)setOverlay:(UIView *)overlay - (void)lt_setBackgroundColor:(UIColor *)backgroundColor { if (!self.overlay) { - [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; - CGFloat statusBarHeight = CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]); - self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + statusBarHeight)]; - self.overlay.userInteractionEnabled = NO; - self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth; // Should not set `UIViewAutoresizingFlexibleHeight` - [[self.subviews firstObject] insertSubview:self.overlay atIndex:0]; + self.overlay = ({ + CGRect rect = CGRectMake(0.0, 0.0, 1.0, 1.0); + UIGraphicsBeginImageContext(rect.size); + CGContextRef context = UIGraphicsGetCurrentContext(); + CGContextSetFillColorWithColor(context, backgroundColor.CGColor); + CGContextFillRect(context, rect); + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + image; + }); } - self.overlay.backgroundColor = backgroundColor; + [self setBackgroundImage:self.overlay forBarMetrics:UIBarMetricsDefault]; } - (void)lt_setTranslationY:(CGFloat)translationY @@ -54,7 +58,7 @@ - (void)lt_setElementsAlpha:(CGFloat)alpha UIView *titleView = [self valueForKey:@"_titleView"]; titleView.alpha = alpha; -// when viewController first load, the titleView maybe nil + // when viewController first load, the titleView maybe nil [[self subviews] enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:NSClassFromString(@"UINavigationItemView")]) { obj.alpha = alpha; @@ -68,7 +72,6 @@ - (void)lt_setElementsAlpha:(CGFloat)alpha - (void)lt_reset { [self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault]; - [self.overlay removeFromSuperview]; self.overlay = nil; }