CNLiveSettingNavigationBar.m
4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// 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