自定义导航栏(支持有左右按钮、标题 或者左按钮、标题 或者 有按钮、标题)

新建类  继承于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







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值