您的位置:首页 > 移动开发 > IOS开发

iOS 导航条透明(以及自定义颜色)的方法,去掉导航条底部黑线,随着tableView滚动设置导航条变色

2017-03-17 13:02 1556 查看
#import
<UIKit/UIKit.h>

#define NAVBAR_CHANGE_POINT30

@interface UINavigationBar (Awesome)

- (void)lt_setBackgroundColor:(UIColor *)backgroundColor;

- (void)lt_setElementsAlpha:(CGFloat)alpha;

- (void)lt_setTranslationY:(CGFloat)translationY;

- (void)lt_reset;

@end

#import
"UINavigationBar+Awesome.h"

#import
<objc/runtime.h

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

@implementation UINavigationBar (Awesome)

staticchar overlayKey;

- (UIView *)overlay

{

   
return objc_getAssociatedObject(self, &overlayKey);

}

- (void)setOverlay:(UIView *)overlay

{

   
objc_setAssociatedObject(self, &overlayKey, overlay,OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

- (void)lt_setBackgroundColor:(UIColor *)backgroundColor

{

    if (!self.overlay) {

        [selfsetBackgroundImage:[UIImagenew]
forBarMetrics:UIBarMetricsDefault];

       
self.overlay = [[UIViewalloc]
initWithFrame:CGRectMake(0,0,
CGRectGetWidth(self.bounds),CGRectGetHeight(self.bounds)
+20)];

       
self.overlay.userInteractionEnabled =NO;

       
self.overlay.autoresizingMask =UIViewAutoresizingFlexibleWidth; 
 // Should not set `UIViewAutoresizingFlexibleHeight`

        [[self.subviewsfirstObject]
insertSubview:self.overlayatIndex:0];

    }

    self.overlay.backgroundColor
= backgroundColor;

}

- (void)lt_setTranslationY:(CGFloat)translationY

{

   
self.transform =CGAffineTransformMakeTranslation(0,
translationY);

}

- (void)lt_setElementsAlpha:(CGFloat)alpha

{

    [[selfvalueForKey:@"_leftViews"]enumerateObjectsUsingBlock:^(UIView
*view,NSUInteger i,
BOOL *stop) {

        view.alpha = alpha;

    }];

    

    [[selfvalueForKey:@"_rightViews"]enumerateObjectsUsingBlock:^(UIView
*view,NSUInteger i,
BOOL *stop) {

        view.alpha = alpha;

    }];

    

    UIView *titleView = [selfvalueForKey:@"_titleView"];

    titleView.alpha = alpha;

//    when viewController first load, the titleView maybe nil

    [[selfsubviews]
enumerateObjectsUsingBlock:^(UIView *obj,NSUInteger idx,
BOOL *stop) {

       
if ([obj isKindOfClass:NSClassFromString(@"UINavigationItemView")])
{

            obj.alpha = alpha;

        }

       
if ([obj isKindOfClass:NSClassFromString(@"_UINavigationBarBackIndicatorView")])
{

            obj.alpha = alpha;

        }

    }];

}

- (void)lt_reset

{

    [selfsetBackgroundImage:nilforBarMetrics:UIBarMetricsDefault];

    [self.overlayremoveFromSuperview];

    self.overlay =nil;

}

@end

#pragma mark ————————调用——————————

#import "UINavigationBar+Awesome.h"

@property (nonatomic,strong)
UIImageView *navBarHairlineImageView;

- (void)viewDidLoad {

    [superviewDidLoad];

   
// 去底部黑线

   
_navBarHairlineImageView = [selffindHairlineImageViewUnder:self.navigationController.navigationBar];

}

- (void)viewWillAppear:(BOOL)animated {

    [superviewWillAppear:animated];

    self.navigationController.navigationBar.translucent
= YES;

    [self.navigationController.navigationBar
lt_setBackgroundColor:[UIColor
clearColor]];

   
_navBarHairlineImageView.hidden =
YES;

    if (_tableView.contentOffset.y
> 30) {

       
// 导航条加颜色(不再透明)

        [self.navigationController.navigationBar
lt_setBackgroundColor:[UIColor
blueColor]];

       
// 滑动范围下移动至导航条下从64开始(确保分区头视图贴着导航条下边缘显示)

       
_tableView.contentInset =
UIEdgeInsetsMake(64,
0, 0,
0);

    } else {

       
self.navigationController.navigationBar.translucent
= YES;

        //
导航条透明

        [self.navigationController.navigationBar
lt_setBackgroundColor:[UIColor
clearColor]];

       
// 滑动范围熊0开始

       
_tableView.contentInset =
UIEdgeInsetsMake(0,
0, 0,
0);

    }

}

-(void)viewWillDisappear:(BOOL)animated {

    [superviewWillDisappear:animated];

    //导航加颜色

    [self.navigationController.navigationBarlt_setBackgroundColor:[UIColorblueColor]];

    //显示底部黑线

    
_navBarHairlineImageView.hidden =NO;

}

// tableView滑动代理方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

   
// tableView 从屏幕最最顶端显示时,当滑动视图下移动至 y > 30时

    if (scrollView.contentOffset.y
> 30) {

         [UIViewanimateWithDuration:0.3animations:^{

            //
导航条加颜色(不再透明)

             [self.navigationController.navigationBarlt_setBackgroundColor:[UIColorclearColor]];

            //
滑动范围下移动至导航条下从64开始(确保分区头视图贴着导航条下边缘显示)

             scrollView.contentInset =UIEdgeInsetsMake(64,0,
0,0);

         }];

    } else {

        [UIViewanimateWithDuration:0.3animations:^{

            //导航条透明

          [self.navigationController.navigationBarlt_setBackgroundColor:[UIColorclearColor]];

            //滑动范围熊0开始

            scrollView.contentInset =UIEdgeInsetsMake(0,0,
0,0);

        }];

    }

}

//去掉导航条底部黑线

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {

   
if ([view isKindOfClass:UIImageView.class]
&& view.bounds.size.height <=1.0)
{

        return (UIImageView *)view;

    }

    for (UIView *subviewin view.subviews)
{

        UIImageView *imageView = [selffindHairlineImageViewUnder:subview];

        if (imageView) {

            return imageView;

        }

    }

   
return nil;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐