新建类 继承于UIView
.h文件
#import <UIKit/UIKit.h>
@interface NavgationView : UIView
@property (nonatomic, strong) UILabel *centerLabel;//中间label
@property (nonatomic, strong) UILabel *rightLabel;//右边label
@property (nonatomic, strong) UIButton *leftBtu;
//导航条只是背景颜色没有任何元素
- (id)initWithNavgationView;
//导航条带标题 点击事件 不需要元素置为nil
- (id)initWithleftAction:(SEL)leftAction centerTitle:(NSString *)centerTitle RightTitlr:(NSString *)rightTitle RightAction:(SEL)rightAction Delegate:(id)delegate;
- (id)initWithCenterTitle:(NSString *)centerTitle RightTitlr:(NSString *)rightTitle RightAction:(SEL)rightAction Delegate:(id)delegate;
@end
.m 文件
#import "NavgationView.h"
#import "UIColor+ColorConversion.h"
@implementation NavgationView
{
id _delegate;
SEL _rightAction;
SEL _leftAction;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (id)initWithNavgationView
{
self = [super initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
self.backgroundColor = [UIColor colorFromHexRGB:@"f57a19"];
return self;
}
- (id)initWithleftAction:(SEL)leftAction centerTitle:(NSString *)centerTitle RightTitlr:(NSString *)rightTitle RightAction:(SEL)rightAction Delegate:(id)delegate{
_rightAction = rightAction;
_delegate = delegate;
_leftAction = leftAction;
self = [self initWithCenterTitle:centerTitle RightTitlr:rightTitle RightAction:rightAction Delegate:delegate];
//返回按钮图标
_leftBtu = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_leftBtu.frame = CGRectMake(20, 20, 50, 44);
[_leftBtu addTarget:self action:@selector(tapLeft) forControlEvents:UIControlEventTouchUpInside];
UIImageView *leftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 11, 12, 21)];
leftView.image = [UIImage imageNamed:@"24x42返回"];
[_leftBtu addSubview:leftView];
UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(17, 14, 30, 15)];
leftLabel.text = @"返回";
leftLabel.textAlignment = NSTextAlignmentLeft;
leftLabel.textColor = [UIColor colorFromHexRGB:@"ffffff"];
leftLabel.font = [UIFont fontWithName:@"HelveticaNeue-light" size:15.0];
[_leftBtu addSubview:leftLabel];
[self addSubview:_leftBtu];
return self;
}
- (id)initWithCenterTitle:(NSString *)centerTitle RightTitlr:(NSString *)rightTitle RightAction:(SEL)rightAction Delegate:(id)delegate
{
_rightAction = rightAction;
_delegate = delegate;
self = [self initWithNavgationView];
if (centerTitle.length != 0) {
_centerLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 20, [UIScreen mainScreen].bounds.size.width-140, 44)];
_centerLabel.text = centerTitle;
_centerLabel.textAlignment = NSTextAlignmentCenter;
_centerLabel.textColor = [UIColor whiteColor];
_centerLabel.font = [UIFont systemFontOfSize:17.0];
[self addSubview:_centerLabel];
}
if (rightTitle.length != 0) {
_rightLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-20-80, 20, 80, 44)];
_rightLabel.text = rightTitle;
_rightLabel.textAlignment = NSTextAlignmentRight;
_rightLabel.textColor = [UIColor whiteColor];
_rightLabel.font = [UIFont systemFontOfSize:17.0];
_rightLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapRightLabel)];
[_rightLabel addGestureRecognizer:tap];
[self addSubview:_rightLabel];
}
return self;
}
- (void)tapLeft
{
if (_leftAction == nil) {
UIViewController *vc = (UIViewController *)_delegate;
NSArray *arr = vc.navigationController.viewControllers;
if (arr.count == 0 || arr.count == 1) {
[vc dismissViewControllerAnimated:YES completion:^{
}];
}else{
[vc.navigationController popViewControllerAnimated:YES];
}
}else{
if (_delegate && [_delegate respondsToSelector:_leftAction]) {
[_delegate performSelector:_leftAction withObject:nil];
}
}
}
- (void)TapRightLabel
{
if (_delegate && [_delegate respondsToSelector:_rightAction]) {
[_delegate performSelector:_rightAction withObject:nil];
}
}
- (void)navigationBarTitle
{
}
@end