zoukankan      html  css  js  c++  java
  • UINavigationBar+Awesome

    目标:

    向上移动控制器时候,出现NavigationBar

    一开始是隐藏NavigationBar的

    如果直接隐藏,然后再上移动出现navigationBar上部会出现一个_UIBackDropView,正是它决定了navigationBar的背景色。

    实现:我们使用associatedObject将overlayView动态地绑定到UINavigationBar的instance上,当调用lt_setBackgroundColor的时候,我们只要更新这个overlayView就行啦~

    #import <UIKit/UIKit.h>

    @interface UINavigationBar (Awesome)

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

    @end

    #import "UINavigationBar+Awesome.h"

    #import <objc/runtime.h>

    @implementation UINavigationBar (Awesome)

    static char 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) {

            [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;

    }

    控制器中需要调用方法,这里实现控制器为tableView控制器

    //

    //  YSBTHomeViewController.m

    //  YSBanTang

    //

    //  Created by Evan on 16/7/1.

    //  Copyright © 2016年 Evan. All rights reserved.

    //

    #define NAVBAR_CHANGE_POINT 44

     #import "YSBTHomeViewController.h"

    static NSString * const YSBTHomeTableViewCellID = @"YSBTHomeTableViewCellID";

    @interface YSBTHomeViewController ()<UITableViewDelegate,UITableViewDataSource>

    @property (nonatomic, weak) UITableView *tableView;

    @end

    @implementation YSBTHomeViewController

    #pragma mark - Datas

    - (UITableView *)tableView

    {

        if (!_tableView)

        {

            UITableView *tableView = [[UITableView alloc] init];

            

            tableView.frame = self.view.frame;

            [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:YSBTHomeTableViewCellID];

            

            self.tableView = tableView;

            [self.view addSubview:tableView];

        }

        return _tableView;

    }

    #pragma mark - Controls

    #pragma mark - Initalize

    #pragma mark - Lifecycle

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        

        self.view.backgroundColor = [UIColor whiteColor];

        

        [self setupNavigation];

        [self setupContentContraints];

        [self.tableView setupDataSourceDelegate:self]; //tableView分类

    }

    #pragma mark - Layout

    - (void)setupNavigation

    {

        [self.navigationController.navigationBar setShadowImage:[UIImage new]];

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

    }

    #pragma mark - UITableView DataSource && Delegate

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {

        return 1;

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        return 100;

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:YSBTHomeTableViewCellID forIndexPath:indexPath];

        

        cell.textLabel.text = @"111";

        return cell;

    }

    #pragma mark - UIScrollView Delegate

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView

    {

        UIColor * color = [UIColor colorWithPatternImage:[UIImage rx_captureImageWithImageName:@"nav_backgroud"]];

        CGFloat offsetY = scrollView.contentOffset.y;

        if (offsetY > NAVBAR_CHANGE_POINT)

        {

            CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + 64 - offsetY) / 64));

            [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];

        } else {

            [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]];

        }

    }

    @end

    完整代码:https://github.com/ltebean/LTNavigationBar

  • 相关阅读:
    Python staticmethod() 函数
    Python open() 函数
    Python input() 函数
    Python divmod() 函数
    Python abs() 函数
    instanceof和类型转换
    多态
    方法重写
    this
    Super详解
  • 原文地址:https://www.cnblogs.com/happyEveryData/p/5634063.html
Copyright © 2011-2022 走看看