做项目,有很多地方都需要自定义导航栏,为了方便管理以及,代码质量,便简单地封装了一个,此导航栏能满足大部分需求,其中,在设置导航栏中间的图片时,需要自己适配一下。为了以后,项目,存档在此。如果,有什么地方,有什么建议,请提出。谢谢!
图例:
[图片上传失败...(image-f94c7d-1513741575076)]
[图片上传失败...(image-bc1386-1513741575076)]
代码示例:
GGCustomNavgationBar.h
#import <UIKit/UIKit.h>
#define K_WIDTH [UIScreen mainScreen].bounds.size.width
#define k_HEIGHT [UIScreen mainScreen].bounds.size.height;
typedef NS_ENUM(NSInteger,GGnavMiddleStyle)
{
GGnavMiddleWithLab = 0, // middle_lab
GGnavMiddleWithimg, // middle_img
};
@protocol GGCustomNavgationBarDelegate <NSObject>
@optional;
- (void)touchTheLeftBtn:(UIButton *)btn;
- (void)touchTheRightBtn:(UIButton *)btn;
@end
@interface GGCustomNavgationBar : UIView
/** delegate*/
@property (nonatomic,weak) id <GGCustomNavgationBarDelegate>delegate;
/** style*/
@property (nonatomic,assign) GGnavMiddleStyle navMiddleStyle;
/** bgcolor*/
@property (nonatomic,strong) UIColor *bgColor;
/** left_nor_img*/
@property (nonatomic,strong) UIImage *leftBtnNorImg;
/** left_select_img*/
@property (nonatomic,strong) UIImage *leftBtnselectImg;
/** left_hightly_img*/
@property (nonatomic,strong) UIImage *leftBtnHightlyImg;
/** right_nor_img*/
@property (nonatomic,strong) UIImage *rightBtnNorImg;
/** right_hightly_img*/
@property (nonatomic,strong) UIImage *rightBtnHightlyImg;
/** middle_img*/
@property (nonatomic,strong) UIImage *middleImage;
/** middle_text_Str*/
@property (nonatomic,copy) NSString *middleTextStr;
/** middle_img_rect*/
@property (nonatomic,assign) CGRect middleImgRect;
@end
#import "GGCustomNavgationBar.h"
@interface GGCustomNavgationBar (
@end
@implementation GGCustomNavgationBar
{
UIButton *_leftBtn;
UIButton *_rightBtn;
UIImageView *_middleImg;
UILabel *_middleText;
UIView *_cotainView;
}
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = _bgColor;
}
return self;
}
#pragma mark - UI布局
- (void)setupSubViews
{
_leftBtn = [[UIButton alloc]initWithFrame:(CGRect){0,20,44,44}];
[_leftBtn addTarget:self action:@selector(clickleftWithbtn:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_leftBtn];
_rightBtn = [[UIButton alloc]init];
[_rightBtn addTarget:self action:@selector(clickRightWithbtn:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_rightBtn];
_rightBtn.frame = CGRectMake(K_WIDTH-44, 20, 44, 44);
if (_navMiddleStyle == GGnavMiddleWithimg) {
_middleImg = [[UIImageView alloc]init];
CGPoint center = _middleImg.center;
center.x = K_WIDTH / 2;
center.y = 44;
_middleImg.center = center;
[self addSubview:_middleImg];
}
if(_navMiddleStyle == GGnavMiddleWithLab){
_middleText = [[UILabel alloc]initWithFrame:(CGRect){0,20,K_WIDTH/2,20}];
CGPoint center = _middleText.center;
center.x = K_WIDTH / 2;
center.y = 44;
_middleText.center = center;
[_middleText setTextColor:[UIColor whiteColor]];
_middleText.textAlignment = NSTextAlignmentCenter;
_middleText.font = [UIFont boldSystemFontOfSize:20];
[self addSubview:_middleText];
}
}
#pragma 点击逻辑处理
- (void)clickRightWithbtn:(UIButton *)sender;
{
if (self.delegate && [self.delegate respondsToSelector:@selector(touchTheRightBtn:)])
{
[self.delegate touchTheRightBtn:sender];
}
}
- (void)clickleftWithbtn:(UIButton *)sender;
{
if(self.delegate && [self.delegate respondsToSelector:@selector(touchTheLeftBtn:)])
{
[self.delegate touchTheLeftBtn:sender];
}
}
#pragma mark - set/get
// set_nar_style
- (void)setNavMiddleStyle:(GGnavMiddleStyle)navMiddleStyle
{
_navMiddleStyle = navMiddleStyle;
[self setupSubViews];
}
// bgColor
- (void)setBgColor:(UIColor *)bgColor
{
_bgColor = bgColor;
self.backgroundColor = bgColor;
}
// left_img_nor
-(void)setLeftBtnNorImg:(UIImage *)leftBtnNorImg
{
_leftBtnNorImg = leftBtnNorImg;
[_leftBtn setImage:leftBtnNorImg forState:UIControlStateNormal];
}
// leftBtnselectImg
- (void)setLeftBtnselectImg:(UIImage *)leftBtnselectImg
{
_leftBtnselectImg = leftBtnselectImg;
[_leftBtn setImage:leftBtnselectImg forState:UIControlStateSelected];
}
// left_hightly_img
- (void)setLeftBtnHightlyImg:(UIImage *)leftBtnHightlyImg
{
_leftBtnHightlyImg = leftBtnHightlyImg;
[_leftBtn setImage:leftBtnHightlyImg forState:UIControlStateHighlighted];
}
// right_img_nor
- (void)setRightBtnNorImg:(UIImage *)rightBtnNorImg
{
_rightBtnNorImg = rightBtnNorImg;
[_rightBtn setImage:rightBtnNorImg forState:UIControlStateNormal];
}
// right_hightly_nor
- (void)setRightBtnHightlyImg:(UIImage *)rightBtnHightlyImg
{
_rightBtnHightlyImg = rightBtnHightlyImg;
[_rightBtn setImage:rightBtnHightlyImg forState:UIControlStateHighlighted];
}
// middle_image
-(void)setMiddleImage:(UIImage *)middleImage
{
_middleImage = middleImage;
_middleImg.image = middleImage;
}
// middleTextStr
-(void)setMiddleTextStr:(NSString *)middleTextStr
{
_middleTextStr = middleTextStr;
_middleText.text = middleTextStr;
}
//middleImgRect
- (void)setMiddleImgRect:(CGRect)middleImgRect
{
_middleImgRect = middleImgRect;
_middleImg.bounds = middleImgRect;
}
@end
基本用法:
#import "GGCustomNavgationBar.h"
#import "GGSecondeController.h"
#define K_WIDTH [UIScreen mainScreen].bounds.size.width
#define k_HEIGHT [UIScreen mainScreen].bounds.size.height;
#define RGBA_GGCOLOR(r,g,b,p) [UIColor colorWithRed:(r) / 255.0f green:(g) / 255.0f blue:(b) / 255.0f alpha:(p)]
@interface GGSecondeController () <GGCustomNavgationBarDelegate>
@end
@implementation GGSecondeController
- (void)viewDidLoad {
[super viewDidLoad];
GGCustomNavgationBar *navBar = [[GGCustomNavgationBar alloc]initWithFrame:(CGRect){0,0,K_WIDTH,64}];
navBar.navMiddleStyle = GGnavMiddleWithimg;
navBar.bgColor = RGBA_GGCOLOR(0, 153, 203, 1);
navBar.leftBtnNorImg = [UIImage imageNamed:@"BackIcon"];
navBar.rightBtnNorImg = [UIImage imageNamed:@"user"];
navBar.middleImage =[UIImage imageNamed:@"MainTitle"];
navBar.middleTextStr = @"订单详情";
CGFloat scale = 0.7;
navBar.middleImgRect = (CGRect){0,0,(K_WIDTH/2)*scale,30*scale};
[self.view addSubview:navBar];
navBar.delegate = self;
}
#pragma mark - 代理方法
- (void)touchTheLeftBtn:(UIButton *)btn
{
NSLog(@"我是左边");
}
- (void)touchTheRightBtn:(UIButton *)btn
{
NSLog(@"我是右边");
}
@end
网友评论