小白随手NavigationBar简单封装
新建一个NSObject 的model
#import <Foundation/Foundation.h>
@interface DzyNavModel : NSObject
@property (nonatomic ) NSString *leftImg;
@property (nonatomic ) NSString *leftBtn;
@property (nonatomic ) NSString *centerImg;
@property (nonatomic ) NSString *centerBtn;
@property (nonatomic ) NSString *rightImg;
@property (nonatomic ) NSString *rightBtn;
@property (nonatomic ) UIColor *color;
@property (nonatomic ) UIImage *backImg;
然后新建一个UIView .h的实现
#import <UIKit/UIKit.h>
#import "DzyNavModel.h"
@protocol DzyNavigationDelegate <NSObject>
- (void)dzyNavLeftWithBtn:(UIButton *)btn;
- (void)dzyNavRightWithBtn:(UIButton *)btn;
@end
@interface DzyNavigation : UIView
@property (nonatomic ) void (^dzyLeft)(UIButton *btn);
@property (nonatomic ) void (^dzyRight)(UIButton *btn);
@property (nonatomic ) id<DzyNavigationDelegate>DzyNavigationDelegate;
@property (nonatomic ) DzyNavModel*model;
@end
.m的实现
#import "DzyNavigation.h"
#import "Masonry.h"
@interface DzyNavigation ()
@property (nonatomic ) UIImageView *leftImg;
@property (nonatomic ) UIButton *leftBtn;
@property (nonatomic ) UIImageView *centerImg;
@property (nonatomic ) UIButton *centerBtn;
@property (nonatomic ) UIImageView *rightImg;
@property (nonatomic ) UIButton *rightBtn;
@property (nonatomic ) UIView *barView;
@end
@implementation DzyNavigation
- (void)toLeft:(UIButton *)btn{
if (self.DzyNavigationDelegate && [self.DzyNavigationDelegate respondsToSelector:@selector(dzyNavLeftWithBtn:)]) {
[self.DzyNavigationDelegate dzyNavLeftWithBtn:btn];
}
if (_dzyLeft) {
_dzyLeft(btn);
}
}
- (void)toRight:(UIButton *)btn{
if (self.DzyNavigationDelegate && [self.DzyNavigationDelegate respondsToSelector:@selector(dzyNavRightWithBtn:)]) {
[self.DzyNavigationDelegate dzyNavRightWithBtn:btn];
}
if (_dzyRight) {
_dzyRight(btn);
}
}
- (void)setModel:(DzyNavModel *)model{
_model = model;
_barView.backgroundColor = model.color;
_barView.layer.contents = (__bridge id _Nullable)(model.backImg.CGImage);
[_leftBtn setTitle:model.leftBtn forState:UIControlStateNormal];
if (![NSString isEmpty:model.leftImg]) _leftImg.image = [UIImage imageNamed:model.leftImg];
[_centerBtn setTitle:model.centerBtn forState:UIControlStateNormal];
_centerImg.image = [UIImage imageNamed:model.centerImg];
[_rightBtn setTitle:model.rightBtn forState:UIControlStateNormal];
_rightImg.image = [UIImage imageNamed:model.rightImg];
CGSize rightSize = CGSizeMake(44, 44);
if ([NSString isEmpty:model.rightBtn]){
}else{
CGSize rightSize = [HubTools changeStationWidth:model.rightBtn andWithWidth:MAXFLOAT orWithHeight:40 anfont:16];
if (rightSize.width>44) {
[_rightBtn updateConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.right).offset(-10);
make.top.equalTo(self).offset(20);
make.height.equalTo(44);
make.width.equalTo(rightSize.width+10);
}];
}
}
CGSize leftSize = CGSizeMake(44, 44);
if ([NSString isEmpty:model.leftBtn]) {
}else{
CGSize leftSize = [HubTools changeStationWidth:model.leftBtn andWithWidth:MAXFLOAT orWithHeight:40 anfont:16];
if (leftSize.width>44) {
[_leftBtn updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(10);
make.top.equalTo(self).offset(20);
make.height.equalTo(44);
make.width.equalTo(leftSize.width+10);
}];
}
}
if (leftSize.width>=rightSize.width) {
[_centerBtn updateConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.width.equalTo(P_w-(leftSize.width+10)*2);
make.top.equalTo(self).offset(20);
make.height.equalTo(44);
}];
}else{
[_centerBtn updateConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.width.equalTo(P_w-(rightSize.width+10)*2);
make.top.equalTo(self).offset(20);
make.height.equalTo(44);
}];
}
}
- (UIImageView *)leftImg{
if (!_leftImg) {
UIImageView *img = [[UIImageView alloc] init];
img.image = [UIImage imageNamed:@"pyq-fh"];
img.contentMode = UIViewContentModeScaleAspectFit;
_leftImg = img;
}
return _leftImg;
}
- (UIImageView *)centerImg{
if (!_centerImg) {
UIImageView *img = [[UIImageView alloc] init];
img.contentMode = UIViewContentModeScaleAspectFit;
_centerImg = img;
}
return _centerImg;
}
- (UIImageView *)rightImg{
if (!_rightImg) {
UIImageView *img = [[UIImageView alloc] init];
img.contentMode = UIViewContentModeScaleAspectFit;
_rightImg = img;
}
return _rightImg;
}
- (UIButton *)leftBtn{
if (!_leftBtn) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.titleLabel.font = [UIFont systemFontOfSize:16];
[btn addTarget:self action:@selector(toLeft:) forControlEvents:UIControlEventTouchUpInside];
_leftBtn = btn;
}
return _leftBtn;
}
- (UIButton *)centerBtn{
if (!_centerBtn) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.titleLabel.font = [UIFont systemFontOfSize:18];
_centerBtn = btn;
}
return _centerBtn;
}
- (UIButton *)rightBtn{
if (!_rightBtn) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.titleLabel.font = [UIFont systemFontOfSize:16];
[btn addTarget:self action:@selector(toRight:) forControlEvents:UIControlEventTouchUpInside];
_rightBtn = btn;
}
return _rightBtn;
}
- (instancetype)initWithFrame:(CGRect)frame{
if (self == [super initWithFrame:frame]) {
[self createUI];
}
return self;
}
- (void)configUI{
[_leftBtn makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(10);
make.top.equalTo(self).offset(20);
make.height.equalTo(44);
make.width.equalTo(44);
}];
[_leftImg makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(_leftBtn);
make.width.and.height.equalTo(20);
}];
[_rightBtn makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.right).offset(-10);
make.top.equalTo(self).offset(20);
make.height.equalTo(44);
make.width.equalTo(44);
}];
[_rightImg makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(_rightBtn);
make.width.and.height.equalTo(10);
}];
[_centerBtn makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.width.equalTo(P_w-108);
make.top.equalTo(self).offset(20);
make.height.equalTo(44);
}];
[_centerImg makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(_centerBtn);
}];
}
- (void)createUI{
UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame:self.bounds];
self.barView = [[UIView alloc]initWithFrame:self.bounds];
self.barView.backgroundColor = [UIColor whiteColor];
[bar addSubview:_barView];
[self addSubview:bar];
[_barView addSubview:self.leftBtn];
[_leftBtn addSubview:self.leftImg];
[_barView addSubview:self.centerBtn];
[_centerBtn addSubview:self.centerImg];
[_barView addSubview:self.rightBtn];
[_rightBtn addSubview:self.rightImg];
[self configUI];
}
@end
中间有两个方法 一个是计算size的
#pragma mark 自动计算高度的类方法
+(CGSize)changeStationWidth:(NSString *)string andWithWidth:(CGFloat)width orWithHeight:(CGFloat)height anfont:(CGFloat)fontSize;{
UIFont * tfont = [UIFont systemFontOfSize:fontSize];
//高度估计文本大概要显示几行,宽度根据需求自己定义。 MAXFLOAT 可以算出具体要多高
CGSize size =CGSizeMake(width,height);
// 获取当前文本的属性
NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
//ios7方法,获取文本需要的size,限制宽度
CGSize actualsize =[string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:tdic context:nil].size;
return actualsize;
}
一个是判断字符串的 类别
#pragma mark 判字符串空
+ (BOOL)isEmpty:(NSString *)string
{
return string == nil || string.length == 0;
}
最后附上用法
leftbtn 默认是给设置一张返回图片 其他看个人需要设置
#import "DzyNavigation.h"
#import "DzyNavModel.h"
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;
#define UIColorFromHex(s) [UIColor colorWithRed:(((s & 0xFF0000) >> 16))/255.0 green:(((s &0xFF00) >>8))/255.0 blue:((s &0xFF))/255.0 alpha:1.0]
//在需要用的地方
DzyNavigation *nav = [[DzyNavigation alloc] initWithFrame:CGRectMake(0, 0, P_w, 64)];
[self.view addSubview:nav];
DzyNavModel *model = [[DzyNavModel alloc] init];
model.color = UIColorFromHex(0x00A29A);
model.centerBtn = @"我的粉丝";
[nav setModel:model];
WS(weakSelf)
[nav setDzyLeft:^(UIButton *btn) {
[weakSelf.navigationController popViewControllerAnimated:YES];
}];
网友评论