美文网首页为了更好的活着
使用runtime自定义NavigationController

使用runtime自定义NavigationController

作者: 劉戦軍 | 来源:发表于2016-06-08 23:16 被阅读161次

使用runtime自定义的NavigationController


这是现在的许多项目中常见的下拉隐藏nav和nav的颜色渐

前提: 需要在stroyboard中给控制器嵌入一个导航控制
  • 这里为大家讲一下自定义的NavigationController
  • 首先, 在ViewController.h中创建一个自定义的UITableView
#import "ViewController.h"
#import "UINavigationController+Extension.h"
#import "ZJTestViewController.h"

static NSString *const ID = @"cell";

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /** 调用自定义navigationBar */
    [self.navigationController.navigationBar setColor:[UIColor redColor] andAlpha:0.0 andScrollView:self.tableView];
    
   
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    NSString *string = [NSString stringWithFormat:@"%ld", indexPath.row];
    
    cell.textLabel.text = string;
    
    return cell;
}
// 在这个方法中可以调用push到下一个控制器的方法
- (IBAction)didClickItemButton:(UIBarButtonItem *)sender {
    
    ZJTestViewController *test = [[ZJTestViewController alloc] init];
    
    [self.navigationController pushViewController:test animated:YES];
}
@end



  • 然后为navigationBar创建一个分类
  • 在分类的.h方法中封装自定义的navigationBar
#import <UIKit/UIKit.h>
@interface UINavigationBar (Extension)

/**
 *  封装自定义navigationBar方法
 *
 *  @param color      需要的颜色
 *  @param alpha      透明度
 *  @param scrollView 需要滚动的控件
 */
- (void)setColor:(UIColor *)color andAlpha:(CGFloat)alpha andScrollView:(UIScrollView *)scrollView;


@end



  • 在.m方法中需要创建所需要的控件和为分类添加属性
#import "UINavigationController+Extension.h"
#import <objc/runtime.h>

static NSString *const customNavkey = @"";

@interface UINavigationBar () <UIScrollViewDelegate>

@property (nonatomic, strong) UIView *alphaView;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIColor *navBarColor;

@end

@implementation UINavigationBar (Extension)

-(UIView *)alphaView {
    // 这是一个获取值的方法
    return objc_getAssociatedObject(self, @selector(alphaView));
}

-(void)setAlphaView:(UIView *)alphaView {
    // 这是一个赋值的方法
    objc_setAssociatedObject(self, @selector(alphaView), alphaView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UIScrollView *)scrollView {
    return objc_getAssociatedObject(self, @selector(scrollView));
}

-(void)setScrollView:(UIScrollView *)scrollView {
     /
     *  使用runtime创建分类属性
     *
     *  @param self       给哪个对象设置属性
        @Selector         这里可以使用_cmd  也可以自定义Key 和使用下面这种方式
     *  @param scrollView 给属性设置的值
     */
    objc_setAssociatedObject(self, @selector(scrollView), scrollView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIColor *)navBarColor {
    return objc_getAssociatedObject(self, @selector(navBarColor));
}

- (void)setNavBarColor:(UIColor *)navBarColor {
    objc_setAssociatedObject(self, @selector(navBarColor), navBarColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setColor:(UIColor *)color andAlpha:(CGFloat)alpha andScrollView:(UIScrollView *)scrollView {
    
    self.navBarColor = color;
    
    self.scrollView = scrollView;
    
    self.scrollView.delegate = self;
    
    if (self.alphaView) {
        self.alphaView.alpha = alpha;
    }else {
        
        UIImage *image = [UIImage new];
        
        [self setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
        
        UIView *alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, 64)];
        
        alphaView.alpha = alpha;
        
        alphaView.backgroundColor = color;
        
        [self setAlphaView:alphaView];
        
        [self insertSubview:alphaView atIndex:0];
        
    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offsetY = scrollView.contentOffset.y;
    
    CGFloat alpha = offsetY / 380;
    
//    [self setColor:self.navBarColor andAlpha:alpha andScrollView:scrollView];
    
    if (offsetY <= 380) {
        
        //        self.navBarView.alpha = alpha;
//        self.alphaView.backgroundColor = [UIColor colorWithRed:255.0/255 green:51.0/255 blue:51.0/255 alpha:alpha];
        
        self.alphaView.alpha = alpha;
        
    }else {
        
        self.alphaView.alpha = 0.8;
//        self.alphaView.backgroundColor = [UIColor colorWithRed:255.0/255 green:51.0/255 blue:51.0/255 alpha:0.8];
    }
    
    NSLog(@"%lf", offsetY);
    
    if (offsetY < 0) {
        
        self.transform = CGAffineTransformMakeTranslation(0, offsetY);
        
    }
}

相关文章

网友评论

    本文标题:使用runtime自定义NavigationController

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