iOS dynamic modification of the navigation bar color UINavigationBar

Examples

 The so-called dynamic modification means that the current page scrolling process or will be triggered in the event return to \ enter a new page  dynamically change the navigation bar

Due to the highly integrated system-level navBar many functions they want implemented if it is very easy getting through inheritance, then, see chapter category category that is relatively more suitable for their own development some personalized custom method to achieve the desired function. 

See http://www.cocoachina.com/ios/20150409/11505.html

This article is the method used to expand the category of an instance method navigationBar even use the c bottom, but it is still very easy to use 

The key category code

//

//  UINavigationBar+MyNavBar.h

//  LTNavigationBar

#import <UIKit/UIKit.h>

 @interface UINavigationBar (MyNavBar)

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

- (void)lt_setElementsAlpha:(CGFloat)alpha;

- (void)lt_setTranslationY:(CGFloat)translationY;

- (void)lt_reset;

@end

//

//  UINavigationBar+Awesome.m

//  LTNavigationBar
#import "UINavigationBar+MyNavBar.h"

#import <objc/runtime.h>

 

@implementation UINavigationBar (MyNavBar)

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;

}

 

- (void)lt_setTranslationY:(CGFloat)translationY

{

    self.transform = CGAffineTransformMakeTranslation(0, translationY);

}

 - (void)lt_setElementsAlpha:(CGFloat)alpha

{

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

        view.alpha = alpha;

    }];

    

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

        view.alpha = alpha;

    }];

    

    UIView *titleView = [self valueForKey:@"_titleView"];

    titleView.alpha = alpha;

//    when viewController first load, the titleView maybe nil

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

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

            obj.alpha = alpha;

            *stop = YES;

        }

    }];

}

- (void)lt_reset

{

    [self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

    [self.overlay removeFromSuperview];

    self.overlay = nil;

}

@end

 Example call this effect also change the transparency of imitation Taobao slide show effect of specific commodities can go to view the page

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource = self;
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
[self setBackButtonStyleLight:YES]; }
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { UIColor * color = [UIColor colorWithRed:0/255.0 green:175/255.0 blue:240/255.0 alpha:1]; CGFloat offsetY =scrollView.contentOffset.y; IF (offsetY> NAVBAR_CHANGE_POINT) { CGFloat Alpha = MIN ( . 1 , . 1 - ((NAVBAR_CHANGE_POINT + 64 - offsetY) / 64 )); // NAVBAR_CHANGE_POINT 64 and see the effect of these two parameters are available demand change is change the timing of transparency as appropriate coefficient [self.navigationController.navigationBar lt_setBackgroundColor: [color colorWithAlphaComponent: alpha]];

         [self setBackButtonStyleLight:NO alpha:alpha];

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

          [self setTitle:@""];

          [self setBackButtonStyleLight:YES];

    }
}

- (void)setBackButtonStyleLight:(BOOL)isLight
{
    [self setBackButtonStyleLight:isLight alpha:1];
}

- (void)setBackButtonStyleLight:(BOOL)isLight alpha:(CGFloat)alpha
{
    if(isLight){
        [titleLabel setAlpha:1];//导航标题
        [backButton setAlpha:1];//返回按钮
        [titleLabel setText:@""];
        UIImage *image = backButton.imageView.image;
        UIImage *targetImage = [UIImage imageNamed:@"icon_fanhi"];
        if(image != targetImage){
            [backButton setImage:[UIImage imageNamed:@"icon_fanhi"] forState:UIControlStateNormal];
            [backButton setImage:[UIImage imageNamed:@"icon_fanhi"] forState:UIControlStateHighlighted];
        }
    }else{
        if(alpha < 0.5){
            [titleLabel setText:@""];
            UIImage *image = backButton.imageView.image;
            UIImage *targetImage = [UIImage imageNamed:@"icon_fanhi"];
            if(image != targetImage){
                [backButton setImage:[UIImage imageNamed:@"icon_fanhi"] forState:UIControlStateNormal];
                [backButton setImage:[UIImage imageNamed:@"icon_fanhi"] forState:UIControlStateHighlighted];
            }
            //先是之前的消失
            [backButton setAlpha:(0.5 - alpha) * 2];
        }else{
            UIImage *image = backButton.imageView.image;
            UIImage *targetImage = [UIImage imageNamed:@"icon_fanhihei"];
            if(image != targetImage){
                [backButton setImage:[UIImage imageNamed:@"icon_fanhihei"] forState:UIControlStateNormal];
                [backButton setImage:[UIImage imageNamed:@"icon_fanhihei"] forState:UIControlStateHighlighted];
            }
            [titleLabel setText:foodItemModel.foodName];
            //将返回和分享慢慢出现
            CGFloat showAlpha = (alpha - 0.5) * 2;
            [titleLabel setAlpha:showAlpha];
            [backButton setAlpha:showAlpha];
        }
    }
}
 
   

- ( void ) the viewWillAppear: (BOOL) Animated { [Super the viewWillAppear: YES]; [Self initScrollNav]; } - ( void ) in viewWillDisappear: (BOOL) Animated { [Super in viewWillDisappear: Animated]; // self.tableView.delegate = nil; // dead code [self.navigationController.navigationBar lt_reset]; // page disappears when to return to normal mode navigation } - ( void ) initScrollNav { self.tableView. the delegate = Self; [Self scrollViewDidScroll: self.tableView]; [self.tableView reloadData]; [self.navigationController.navigationBar setShadowImage: [UIImage new new ]]; } - ( void ) the dealloc { . self.tableView delegate = nil; // not nil this time will still trigger scrollViewDidLoad in viewWilldisappear in the crash dump needs to write here }

Another case is in the process of rolling the navigation bar disappears some of the more popular products in particular, I personally do not like I have been convinced after ios7 Apple is to promote the integration of the navigation bar status bar status bar so leave do you think is the Android status bar to retain this situation? ugly burst clear understanding of the rights and eggs only advise the developer no power to decide how to achieve it is still to be

Example:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY > 0) {
        if (offsetY >= 44) {
            [self setNavigationBarTransformProgress:1];
        } else {
            [self setNavigationBarTransformProgress:(offsetY / 44)];
        }
    } else {
        [self setNavigationBarTransformProgress:0];
        self.navigationController.navigationBar.backIndicatorImage = [UIImage new];
    }
}

- (void)setNavigationBarTransformProgress:(CGFloat)progress
{
    [self.navigationController.navigationBar lt_setTranslationY:(-44 * progress)];
    [self.navigationController.navigationBar lt_setElementsAlpha:(1-progress)];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];//保留状态栏的那种情况
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar lt_reset];
}

Reproduced in: https: //www.cnblogs.com/someonelikeyou/p/5124403.html

Guess you like

Origin blog.csdn.net/weixin_34240657/article/details/94538165