美文网首页
关于设置navbar颜色色差的解决办法

关于设置navbar颜色色差的解决办法

作者: butterflyer | 来源:发表于2017-01-18 11:22 被阅读594次

    对于这个改变色差的方法,以前一直纠结,一直想凑合。。今天终于是凑合不起来了。各种试各种改都达不到要求。最后终于找到解决办法。下面贴出解决方案,实际上就是给navbar添加个分类。

    import "UINavigationBar+BackgroundColor.h"

    static char overlayKey;

    @implementation UINavigationBar (BackgroundColor)

    • (UIView *)overlay{
      return objc_getAssociatedObject(self, &overlayKey);
      }

    • (void)setOverlay:(UIView *)overlay{
      objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
      }

    • (void)KPSetBackgroundColor:(UIColor *)backgroundColor{
      backgroundColor = _COLOR_RGB(0xe72a2d);

      if (!self.overlay) {
      [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

        UIView *backgroundView = [self KPGetBackgroundView];
        self.overlay = [[UIView alloc] initWithFrame:backgroundView.bounds];
        self.overlay.userInteractionEnabled = NO;
        self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        
        [backgroundView insertSubview:self.overlay atIndex:0];
      

      }
      self.overlay.backgroundColor = backgroundColor;
      }

    • (UIView*)KPGetBackgroundView{
      //iOS10之前为 _UINavigationBarBackground, iOS10为 _UIBarBackground
      //_UINavigationBarBackground实际为UIImageView子类,而_UIBarBackground是UIView子类
      //之前setBackgroundImage直接赋值给_UINavigationBarBackground,现在则是设置后为_UIBarBackground增加一个UIImageView子控件方式去呈现图片

      if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
      UIView *_UIBackground;
      NSString *targetName = @"_UIBarBackground";
      Class _UIBarBackgroundClass = NSClassFromString(targetName);

        for (UIView *subview in self.subviews) {
            if ([subview isKindOfClass:_UIBarBackgroundClass.class]) {
                _UIBackground = subview;
                break;
            }
        }
        return _UIBackground;
      

      }
      else {
      UIView *_UINavigationBarBackground;
      NSString *targetName = @"_UINavigationBarBackground";
      Class _UINavigationBarBackgroundClass = NSClassFromString(targetName);

        for (UIView *subview in self.subviews) {
            if ([subview isKindOfClass:_UINavigationBarBackgroundClass.class]) {
                _UINavigationBarBackground = subview;
                break;
            }
        }
        return _UINavigationBarBackground;
      

      }
      }

    pragma mark - shadow view

    • (void)KPHideShadowImageOrNot:(BOOL)bHidden{
      UIView *bgView = [self KPGetBackgroundView];

      //shadowImage应该是只占一个像素,即1.0/scale
      for (UIView *subview in bgView.subviews) {
      if (CGRectGetHeight(subview.bounds) <= 1.0) {
      subview.hidden = bHidden;
      }
      }
      }

    @end
    self.navigationController.navigationBar.translucent = YES;
    要用的直接贴上去就可以。

    http://www.cocoachina.com/ios/20161214/18353.html

    相关文章

      网友评论

          本文标题:关于设置navbar颜色色差的解决办法

          本文链接:https://www.haomeiwen.com/subject/bggvbttx.html