CNLiveSettingNavigationBar.m 4.27 KB
//
//  CNLiveSettingNavigationBar.m
//  CNLiveSettingModule
//
//  Created by 153993236@qq.com on 11/12/2019.
//  Copyright © 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveSettingNavigationBar.h"
#import "QMUIKit.h"

@interface CNLiveSettingNavigationBar ()

@end

@implementation CNLiveSettingNavigationBar
static const NSInteger margin = 15;

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor clearColor];
        [self createSubViews];
    }
    return self;
}

- (void)dealloc {
    NSLog(@"CNLiveSettingNavigationBar -- dealloc");
}

- (void)createSubViews {
    [self addSubview:self.navView];
    [self addSubview:self.left];
    [self addSubview:self.title];
    [self addSubview:self.right];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

#pragma mark - 懒加载
- (UIView *)navView {
    if (!_navView) {
        _navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, NavigationContentTop)];
        _navView.backgroundColor = [UIColor clearColor];
    }
    return _navView;
}

- (UIButton *)left {
    if (!_left) {
        _left= [UIButton buttonWithType:UIButtonTypeCustom];
        _left.adjustsImageWhenHighlighted = NO;
        _left.frame = CGRectMake(margin, StatusBarHeight, NavigationBarHeight, NavigationBarHeight);
        UIImage *image = [self xw_getImageName:@"mine_back_black_b" bundleName:@"CNLiveSettingModule" targetClass:[CNLiveSettingNavigationBar class]];
        [_left setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        _left.titleLabel.font = [UIFont systemFontOfSize:15];
        [_left setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [_left addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    }
    return _left;
}

- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] initWithFrame:CGRectMake(margin+NavigationBarHeight, StatusBarHeight, SCREEN_WIDTH-2*(margin+NavigationBarHeight), NavigationBarHeight)];
        [_title setFont:[UIFont systemFontOfSize:18]];
        _title.textColor = [UIColor blackColor];
        _title.textAlignment = NSTextAlignmentCenter;
    }
    return _title;
}

- (UIButton *)right {
    if (!_right) {
        _right = [UIButton buttonWithType:UIButtonTypeCustom];
        _right.frame = CGRectMake(SCREEN_WIDTH-NavigationBarHeight-margin, StatusBarHeight, NavigationBarHeight, NavigationBarHeight);
        [_right setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        _right.adjustsImageWhenHighlighted = NO;
        _right.titleLabel.font = [UIFont systemFontOfSize:15];
        [_right setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_right addTarget:self action:@selector(rightDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _right;
}

#pragma mark - 响应方法
- (void)goBack{
    if (self.delegate && [self.delegate respondsToSelector:@selector(navigationBar:actionEvents:)]) {
        [self.delegate navigationBar:self actionEvents:@"1"];
    }
}
- (void)rightDidClicked:(UIButton *)btn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(navigationBar:actionEvents:)]) {
        [self.delegate navigationBar:self actionEvents:@"2"];
    }
}

/**
 *  创建图片
 *
 *  @param imageName 图片名字
 *  @param bundleName 图片所在的bundle名字
 *  @param targetClass 类和bundle的同级目录
 *  @return 返回UIImage
 */
- (UIImage *)xw_getImageName:(NSString *)imageName bundleName:(NSString *)bundleName targetClass:(Class)targetClass{
    NSBundle *bundle = [NSBundle bundleForClass:targetClass];
    NSURL *url = [bundle URLForResource:bundleName withExtension:@"bundle"];
    NSBundle *targetBundle = [NSBundle bundleWithURL:url];
    UIImage *image = [UIImage imageNamed:imageName inBundle:targetBundle compatibleWithTraitCollection:nil];
    return image?image:[UIImage imageNamed:imageName inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
    
}

@end