1. 程式人生 > >導航欄漸變透明&下拉image放大

導航欄漸變透明&下拉image放大

1、首先使用分類 對UINavigationBar 進行類擴充套件

@interface UINavigationBar (Background)

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

- (void)cnReset;

@end

#import "UINavigationBar+Background.h"
#import <objc/runtime.h>

@implementation UINavigationBar (Background)

static char overlayKey;

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

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

- (void)cnSetBackgroundColor:(UIColor *)backgroundColor{
    if (!self.overlay) {
        [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, CGRectGetHeight(self.bounds)+20)];
        self.overlay.userInteractionEnabled = NO;
        self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self insertSubview:self.overlay atIndex:0];
    }
    self.overlay.backgroundColor = backgroundColor;
}

/**
 *  釋放
 */
- (void)cnReset{
    [self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.overlay removeFromSuperview];
     self.overlay = nil;
}

2、在當前控制器中寫入對UINavigationBar 與 圖片的處理

#import "XXTableViewViewController.h"
#import "UINavigationBar+Background.h"

#define SCREEN_RECT [UIScreen mainScreen].bounds
static const CGFloat headerImageHeight = 260.0f;

@interface XXTableViewViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    // 拉伸的底圖
    UIImageView *headerImageView;
}
@end

@implementation XXTableViewViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.mTableView.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.mTableView.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"呵呵呵呵~~";
    
    //1、 去掉navigationbar 上的背景圖片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //1.1、 去掉navigationbar 上圖片下面的線
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    
    self.mTableView.contentInset = UIEdgeInsetsMake(headerImageHeight, 0, 0, 0);
    self.mTableView.tableHeaderView = [[UIView alloc] init];
    
    
    headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,
                                                                    -headerImageHeight,
                                                                    CGRectGetWidth(SCREEN_RECT), headerImageHeight)];
    headerImageView.image = [UIImage imageNamed:@"headerImage"];
    // 高度改變  寬高也跟著改變
    headerImageView.contentMode = UIViewContentModeScaleToFill;//重點(不設定拿獎只會被縱向拉伸)
    // 設定autoresizesSubView讓子類自動佈局。
    headerImageView.autoresizesSubviews = YES;
    [self.mTableView addSubview:headerImageView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *string = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"========>%ld",(long)indexPath.row];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    /**
     *  處理頭部圖片拉伸放大效果
     */
    CGFloat y = scrollView.contentOffset.y + 64;
    if (y < -headerImageHeight) {
        CGRect rect = headerImageView.frame;
        rect.origin.y = y;
        rect.size.height =  - y;
        headerImageView.frame = rect;
    }
    
    /**
     *  處理Navigationbar 背景圖片漸變效果
     */
    UIColor *color = [UIColor colorWithRed:0.5 green:0.5 blue:1 alpha:1];
    CGFloat offsetY = scrollView.contentOffset.y + headerImageHeight;
    // Y軸大於0 設定navigationbar的透明度
    if (offsetY >0) {
        CGFloat alpha = MIN(1, offsetY/(headerImageHeight - 64));
        [self.navigationController.navigationBar cnSetBackgroundColor:[color colorWithAlphaComponent:alpha]];
     
        [self setNavigationColor:offsetY];
        
    } else {
        [self.navigationController.navigationBar cnSetBackgroundColor:[color colorWithAlphaComponent:0]];
        [self setNavigationColor:offsetY];
    }
}

#pragma mark 重寫狀態列的方法
/**
 
 為了適配IOS9 需要在 .pList檔案裡新增
 View controller-based status bar appearance 設定為 NO  
 預設為 YES
 
 */
- (void)setNavigationColor:(CGFloat)floatY{
    if (floatY >= 180) {
        //狀態列˝˝
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        //標題顏色
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
        //導航欄子控制元件顏色
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        
    }else{
        //狀態列
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
        //標題顏色
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor]};
        //導航欄子控制元件顏色
        self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

注意:當離開介面需要把TableView的代理方法取消掉 ,因為如果不取消掉的話 離開介面後還是會呼叫一次 scrollViewDidScroll 方法。


Demo 地址:https://github.com/xiangxianxiao/XXNavigationBarColor