iOS 自定义导航栏 简单好使用

自定义导航栏,就是隐藏系统的导航栏,自己添加一个view。

 

系统的导航栏可以在 父类的 NavViewController中一次设置:

    self.navigationBarHidden=YES;

也可以在每一个controller中设置:

[self.navigationController setNavigationBarHidden:YES animated:animated];

-

-

-

-

自定义一个 view类,NavView继承自:UIView

.h文件


#import <UIKit/UIKit.h>

@protocol navViewDelegate<NSObject>
@optional
/**
 *导航的返回
 */
- (void)navBackClicked;

/**
 *导航右边备用按钮
 */
- (void)navRightClicked;
@end

@interface NavView : UIView
@property (nonatomic,weak)id<navViewDelegate>delegate;

/**
 初始化自定义导航栏

 @param names 导航栏中心title
 @param color 导航栏背景颜色
 @param image 导航栏左边图片
 @param imager 导航浪右边图片
 @param titles 导航栏右边图片名字
 @param frame 位置
 @param delegate 导航左右图片点击代理
 @return navView
 */
- (id)initWithTitle:(NSString *)names withBgColor:(UIColor *)color withLeftImage:(NSString *)image withRightImage:(NSString *)imager withRighttitle:(NSString *)titles withHiddenLine:(BOOL)hiddenLine theFrame:(CGRect)frame theDelegate:(id<navViewDelegate>)delegate;
@property (nonatomic,strong)NSString *title,*leftimage,*rightimage,*righttitle;
/*是否显示导航栏右边图片*/
@property (nonatomic,assign)BOOL rightimageShow;
/*是否显示下划线*/
@property (nonatomic,assign)BOOL hiddenLine;

.m 文件


#import "NavView.h"
@interface NavView()
@property (nonatomic,strong)UILabel *navtitle;
@property (nonatomic,strong)UILabel *line;
@property (nonatomic,strong)UIImageView *rightImage;

@end
@implementation NavView

- (id)initWithTitle:(NSString *)names withBgColor:(UIColor *)color withLeftImage:(NSString *)image withRightImage:(NSString *)imager withRighttitle:(NSString *)titles withHiddenLine:(BOOL)hiddenLine theFrame:(CGRect)frame theDelegate:(id<navViewDelegate>)delegate{
    self=[super init];
    if(self){
        self.delegate = delegate;
        self.frame = frame;
        self.title = names;
        self.leftimage = image;
        self.rightimage = imager;
        self.righttitle = titles;
        self.hiddenLine = hiddenLine;
        self.backgroundColor = color;
        [self createView:names];
    }
    return self;
}
- (void)createView:(NSString *)title{
    
    UIView *backBg=[[UIView alloc]init];
    [self addSubview:backBg];
    [backBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(60);
    }];
    UIImageView *backimage=[[UIImageView alloc]init];
    [backBg addSubview:backimage];
    backimage.image=[UIImage imageNamed:self.leftimage];
    backimage.userInteractionEnabled=YES;
    [backimage mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.left.mas_equalTo(backBg).offset(15);
        make.width.height.mas_equalTo(15);  //26.44
        make.center.mas_equalTo(backBg);
        //        make.bottom.mas_equalTo(backBg).mas_offset(-12);
        
    }];
    UITapGestureRecognizer *imagetap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backClick:)];
    [backBg addGestureRecognizer:imagetap];
    
    UIView *rightBg=[[UIView alloc]init];
    [self addSubview:rightBg];
    [rightBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(50);
    }];
    UIImageView *rightimage=[[UIImageView alloc]init];
    /*xgHelpManager getBtnwithBackgroundColor 自己定义的一个创建按钮的方法*/
    UIButton *rightbtn=[xgHelpManager getBtnwithBackgroundColor:nil withFont:[TiUIFont get30Font] withCornerRadius:0 withTitle:self.righttitle withTitleColor:[TiUIColor getMainColor]];
    /*
     当导航栏右边title有值时即表示是:文本按钮
     当导航栏右边image有值时即表示是:图片按钮
     */
    if(self.righttitle.length>0){
        [rightbtn addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
        [rightBg addSubview:rightbtn];
        [rightbtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self).offset(-5);
            make.width.height.mas_equalTo(44);
            make.centerY.mas_equalTo(backimage);
            
        }];
    }else if(self.rightimage.length>0){
        [rightBg addSubview:rightimage];
        self.rightImage=rightimage;
        UITapGestureRecognizer *imageRight=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(rightClick)];
        [rightBg addGestureRecognizer:imageRight];
        rightimage.userInteractionEnabled=YES;
        rightimage.image=[UIImage imageNamed:self.rightimage];
        [rightimage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self).offset(-15);
            make.width.height.mas_equalTo(20);
            make.centerY.mas_equalTo(backimage);
            
        }];
    }
    /*
     导航栏中间title
     
     */
    UILabel *label=[[UILabel alloc]init];
    [self addSubview:label];
    label.textColor=[TiUIColor getMainColor];
    label.font=[TiUIFont getFont:18];
    label.text=self.title;
    label.textAlignment=NSTextAlignmentCenter;
    self.navtitle=label;
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(80);
        make.height.mas_equalTo(20);
        make.bottom.mas_equalTo(self).mas_offset(-12);
        make.right.mas_equalTo(-80);
    }];
    
    /*
     下划线
     */
    
    self.line=[xgHelpManager getline];
    [self addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    
    self.line.hidden = self.hiddenLine;
    
}
- (void)backClick:(UITapGestureRecognizer *)sender{
    if (self.delegate &&[self.delegate respondsToSelector:@selector(navBackClicked)]) {
        [self.delegate navBackClicked];
    }
}
- (void)rightClick{
    if(self.rightimageShow||self.righttitle.length>0){
        if (self.delegate &&[self.delegate respondsToSelector:@selector(navRightClicked)]) {
            [self.delegate navRightClicked];
        }
    }
}
- (void)setTitle:(NSString *)title{
    _title=title;
    self.navtitle.text=_title;
}
- (void)setRightimageShow:(BOOL)rightimageShow{
    _rightimageShow=rightimageShow;
    if(_rightimageShow){
        self.rightImage.hidden=NO;
    }else{
        self.rightImage.hidden=YES;
    }
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end

调用:

    self.navView=[[NavView alloc] initWithTitle:@"我的订单" withBgColor:[UIColor whiteColor] withLeftImage:@"back-hei" withRightImage:@"" withRighttitle:@"" withHiddenLine:NO theFrame:CGRectMake(0, 0, WIDTH, HYDevice_NaviBar_Height) theDelegate:self];
/*nav 代理*/
- (void)navBackClicked{
    [self.navigationController popViewControllerAnimated:YES];
}

下载地址 https://github.com/huangxiaolin0425/NavView

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值