扩展 导航 view 视图
2022-03-01 本文已影响0人
失忆的程序员
// 导航视图
#import "BaseView.h"
NS_ASSUME_NONNULL_BEGIN
@interface NavigationBar : BaseView
/// 标题Label,通过类方法传入title初始化,
@property (nonatomic, weak/*, readonly*/) UILabel *titleLabel;
/// 自定义view,通过类方法初始化, 只读
@property (nonatomic, weak, readonly) UIView *customView;
/// 右侧按钮集合,通过类方传入rightItems法初始化,只读
@property (nonatomic, copy, readonly) NSArray<UIButton *> *rightButtons;
/// 右侧按钮
@property (nonatomic, weak) UIButton *rightButton;
/// 返回按钮
@property (nonatomic, weak) UIButton *leftButton;
/// 分隔线
@property (nonatomic, weak) UIView *diveder;
@property (nonatomic, copy) void (^rightBtnAction)(NSInteger index);
/// 设置成白色主题
- (void)setupToWhiteTheme;
/// 设置自定义颜色主题
///
/// @param titleColor 文字色
/// @param backimgStr 返回图片
- (void)setUpUITitleColor:(UIColor *)titleColor backimg:(NSString *)backimgStr;
/// 只有标题
///
/// @param title 标题
///
/// @return 导航栏
+ (instancetype)navWithTitle:(NSString *)title;
/// 只有标题和返回键
///
/// @param title 标题
/// @param backAction 返回事件
///
/// @return 导航栏
+ (instancetype)navWithTitle:(NSString *)title
backAction:(void(^)(void))backAction;
/// 标题 + 右边item(图片或文字都行)(没有返回按钮)
/// @return 导航栏
+ (instancetype)navWithTitle:(NSString *)title
rightItem:(NSString *)rightItem
rightAction:(void(^)(void))action;
/// 标题 + 右边item(图片或文字都行) + 返回按钮
///
/// @param title 标题
/// @param rightItem 右侧图片/文字
/// @param action 右侧点击事件
/// @param backAction 返回事件
///
/// @return 导航栏
+ (instancetype)navWithTitle:(NSString *)title
rightItem:(NSString *)rightItem
rightAction:(void(^)(void))action
backAction:(void(^)(void))backAction;
/// 标题 + 右边item(图片或文字都行) + 返回按钮
///
/// @param customView 自定义view
/// @param rightItem 右侧图片/文字
/// @param action 右侧点击事件
/// @param backAction 返回事件
///
/// @return 导航栏
+ (instancetype)navWithCustomView:(UIView *)customView
rightItem:(NSString *)rightItem
rightAction:(void(^)(void))action
backAction:(void(^)(void))backAction;
/// 标题 + 右边items(图片或文字都行) + 返回按钮
///
/// @param title 标题
/// @param rightItems 右侧图片/文字
/// @param action 右侧点击事件
/// @param backAction 返回事件
///
/// @return 导航栏
+ (instancetype)navWithTitle:(NSString *)title
rightItems:(NSArray<NSString *> *)rightItems
rightAction:(void(^)(NSInteger index))action
backAction:(void(^)(void))backAction;
//////////新增
/**
自定义左右按钮
*/
+ (instancetype)navWithTitle:(NSString *)title
lefItem:(id)leftItem
leftAction:(void(^)(void))leftAction
rightItem:(NSString *)rightItem
rightAction:(void(^)(void))rightAction;
//////////新增
/**
自定义视图自定义尺寸
*/
+ (instancetype)navWithCustomView:(UIView *)customView
frame:(CGRect)frame
backAction:(void(^)(void))backAction;
//////////新增
/**
自定义视图 customView
*/
+ (instancetype)navWithCustomView:(UIView *)customView frame:(CGRect)frame;
///
/**
自定义视图 color
*/
- (void)addTransitionColorType:(TransitionColorType)type startColor:(UIColor *)startColor endColor:(UIColor *)endColor;
/**
自定义视图 color
*/
- (void)addTransitionStartColor:(UIColor *)start endColor:(UIColor *)end;
/// 删除分隔线
- (void)removeDivider;
@end
@interface UIViewController (NavigatiionBar)
@property (nonatomic, strong) NavigationBar *rm_navgationBar;
@end
NS_ASSUME_NONNULL_END
//
#import <objc/runtime.h>
#import "NavigationBar.h"
#import "UIView+TransitonColor.h"
/// 返回按钮图片
static NSString *BackButtonImageName = @"back_black";
/// 标题文字大小
static CGFloat TitleFont = 18;
/// 按钮文字大小
static CGFloat ButtonFont = 15;
typedef void (^ButtonClick)(void);
@interface NavigationBar ()
@property (nonatomic, copy) ButtonClick backBlock;
@property (nonatomic, copy) ButtonClick actionBlock;
@end
@implementation NavigationBar {
// UIView *_diveder;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIInterfaceOrientation sataus = [UIApplication sharedApplication].statusBarOrientation;
if (sataus == UIInterfaceOrientationLandscapeRight)
{
self.size = Size(SCREENWIDTH, NAVBARHEIGHT);
}
else
{
self.size = Size(SCREENHEIGHT, NAVBARHEIGHT);
}
//[self addTransitionColor:DGColor0 endColor:DGColor1];
// 分隔线
UIView *diveder = [UIView new];
diveder.frame = Rect(0, NAVBARHEIGHT - 0.5, SCREENWIDTH, 0.5);
diveder.backgroundColor = Color_EE_x;//DGColorInt32_t(307859);
[self addSubview:diveder];
[self bringSubviewToFront:diveder];
_diveder = diveder;
}
return self;
}
#pragma mark - 颜色
- (void)addTransitionColorType:(TransitionColorType)type startColor:(UIColor *)startColor endColor:(UIColor *)endColor
{
[self addTransitionColorType:type startColor:startColor endColor:endColor];
}
- (void)addTransitionStartColor:(UIColor *)start endColor:(UIColor *)end
{
[self addTransitionColor:start endColor:end];
}
- (void)removeDivider
{
[_diveder removeFromSuperview];
}
/// 自定义 大小
- (instancetype)initWithCustomViewFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIInterfaceOrientation sataus = [UIApplication sharedApplication].statusBarOrientation;
if (sataus == UIInterfaceOrientationLandscapeRight)
{
self.size = frame.size;
}
else
{
self.size = frame.size;
}
//[self addTransitionColor:DGColor0 endColor:DGColor1];
// 分隔线
UIView *diveder = [UIView new];
diveder.frame = Rect(0, frame.size.height - 0.5, frame.size.width, 0.5);
diveder.backgroundColor = DGColorInt32_t(D2D2D2);
[self addSubview:diveder];
[self bringSubviewToFront:diveder];
_diveder = diveder;
}
return self;
}
- (instancetype)initWithObject:(id)obj rights:(NSArray *)rights rightAction:(void (^)(NSInteger))action backAction:(void (^)(void))backAction {
if (self = [super init])
{
CGFloat centerY = 20 + (NAVBARHEIGHT - 20) / 2;
// 标题
UIView *centerView = nil;
if ([obj isKindOfClass:NSString.class])
{
UILabel *label = [UILabel labelWithText:obj font:TitleFont textColor:ThemeTitleColor frame:Rect(45, NAVBARHEIGHT - 44, SCREENWIDTH - 90, 42)];
label.font = [UIFont boldSystemFontOfSize:TitleFont];
[self addSubview:label];
_titleLabel = (UILabel *)label;
label.textAlignment = NSTextAlignmentCenter;
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
centerView = label;
}
else if ([obj isKindOfClass:UIView.class])
{
UIView *objView = obj;
[self addSubview:objView];
if (objView.height > 44) {
objView.height = 44;
}
if (objView.width > SCREENWIDTH - 120) {
objView.width = SCREENWIDTH - 120;
}
objView.center = Point(SCREENWIDTH/2.0f, centerY);
centerView = objView;
_customView = objView;
}
// 如果实现了右边的事件,会创建右边的按钮
if (action)
{
CGFloat maxX = SCREENWIDTH - 7;
NSMutableArray *btns = @[].mutableCopy;
for (int i = 0; i < rights.count; i++) {
NSString *right = rights[i];
UIButton *rightBtn = nil;
UIImage *image = [UIImage rm_imageNamed:right];
if (image)
{
if (i == 0) maxX -= 3;
rightBtn = [UIButton buttonWithTitle:nil titleColor:[UIColor whiteColor] backgroundColor:nil font:0 image:right frame:Rect(0, HeightStatusBar, 44, 44)];
[self addSubview:rightBtn];
rightBtn.maxX = maxX;
maxX = rightBtn.x;
}
else
{
if (i == 0) maxX -= 8;
if ([right length])
{
CGSize size = [NSString getStringRect:right fontSize:ButtonFont width:300];
rightBtn = [UIButton buttonWithTitle:right titleColor:[UIColor blackColor] backgroundColor:nil font:ButtonFont image:nil frame:Rect(0, HeightStatusBar, size.width, 44)];
[self addSubview:rightBtn];
rightBtn.maxX = maxX;
maxX = rightBtn.x - 7;
}
}
rightBtn.tag = i;
[rightBtn addTarget:self action:@selector(rightBtnClick:)];
self.rightBtnAction = action;
if (rightBtn) [btns addObject:rightBtn];
}
if (btns.count == 1)
{
self.rightButton = [btns lastObject];
}
else
{
_rightButtons = [btns copy];
}
}
// 如果实现了返回的事件,才创建返回按钮
if (backAction)
{
UIButton *back = [UIButton buttonWithTitle:nil titleColor:nil backgroundColor:nil font:ButtonFont image:BackButtonImageName frame:Rect(0, HeightStatusBar, 44, 44)];
[back addTarget:self action:@selector(backClick)];
back.adjustsImageWhenHighlighted = NO;
[self addSubview:back];
self.backBlock = backAction;
self.leftButton = back;
}
}
return self;
}
#pragma mark - ButtonAction
- (void)btnClick {
!self.actionBlock ? : self.actionBlock();
}
- (void)backClick {
!self.backBlock ? : self.backBlock();
}
- (void)rightBtnClick:(UIButton *)btn {
btn.selected = !btn.selected;
BLOCK_SAFE_RUN(self.rightBtnAction, btn.selected);
}
- (void)setupToWhiteTheme {
self.backgroundColor = [UIColor clearColor];
self.titleLabel.textColor = DGColorInt32_t(000000);
self.rightButton.titleColor = DGColorInt32_t(333333);
self.leftButton.image = [UIImage imageNamed:@"back_black"];
[_diveder removeFromSuperview];
}
- (void)setUpUITitleColor:(UIColor *)titleColor backimg:(NSString *)backimgStr {
self.titleLabel.textColor = titleColor;
self.leftButton.image = [UIImage imageNamed:backimgStr];
[_diveder removeFromSuperview];
}
#pragma mark - 类方法
+ (instancetype)navWithTitle:(NSString *)title rightItem:(NSString *)rightItem rightAction:(void (^)(void))action {
NavigationBar *nav = [[self alloc] initWithObject:title rights:rightItem.length ? @[rightItem] :nil rightAction:^(NSInteger index) {
BLOCK_SAFE_RUN(action);
} backAction:nil];
return nav;
}
+ (instancetype)navWithTitle:(NSString *)title {
// return [[self alloc] initWithObject:title right:nil rightAction:nil backAction:nil];
NavigationBar *nav = [[self alloc] initWithObject:title rights:nil rightAction:nil backAction:nil];
return nav;
}
+ (instancetype)navWithTitle:(NSString *)title backAction:(void (^)(void))backAction {
// return [[self alloc] initWithObject:title right:nil rightAction:nil backAction:backAction];
NavigationBar *nav = [[self alloc] initWithObject:title rights:nil rightAction:nil backAction:backAction];
return nav;
}
+ (instancetype)navWithTitle:(NSString *)title rightItem:(NSString *)rightItem rightAction:(void (^)(void))action backAction:(void (^)(void))backAction {
// return [[self alloc] initWithObject:title right:rightItem rightAction:action backAction:backAction];
NavigationBar *nav = [[self alloc] initWithObject:title rights:rightItem.length ? @[rightItem] :nil rightAction:^(NSInteger index) {
BLOCK_SAFE_RUN(action);
} backAction:backAction];
return nav;
}
+ (instancetype)navWithCustomView:(UIView *)costomView rightItem:(NSString *)rightItem rightAction:(void (^)(void))action backAction:(void (^)(void))backAction {
// return [[self alloc] initWithObject:costomView right:rightItem rightAction:action backAction:backAction];
NavigationBar *nav = [[self alloc] initWithObject:costomView rights:rightItem.length ? @[rightItem] :nil rightAction:^(NSInteger index) {
BLOCK_SAFE_RUN(action);
} backAction:backAction];
return nav;
}
+ (instancetype)navWithTitle:(NSString *)title rightItems:(NSArray<NSString *> *)rightItems rightAction:(void(^)(NSInteger index))action backAction:(void(^)(void))backAction {
return [[self alloc] initWithObject:title rights:rightItems rightAction:action backAction:backAction];
}
//自定义左右按钮
+ (instancetype)navWithTitle:(NSString *)title
lefItem:(id)leftItem
leftAction:(void(^)(void))leftAction rightItem:(NSString *)rightItem rightAction:(void(^)(void))rightAction
{
NavigationBar *nav = [[self alloc] initWithObject:title rights:rightItem.length ? @[rightItem] : nil rightAction:^(NSInteger index) {
BLOCK_SAFE_RUN(rightAction);
} backAction:leftAction];
UIImage *img = nil;
if ([leftItem isKindOfClass:[NSString class]])
{
img = DGIMGName(leftItem);
}
else
{
img = leftItem;
}
if (img)
{
[nav.leftButton setImage:img forState:UIControlStateNormal];
}
else if (!img && [leftItem isKindOfClass:NSString.class])
{
[nav.leftButton setImage:nil forState:UIControlStateNormal];
[nav.leftButton setTitle:leftItem forState:UIControlStateNormal];
[nav.leftButton sizeToFit];
nav.leftButton.centerY = nav.titleLabel.centerY;
}
return nav;
}
/// 新增 - 自定义视图自定义尺寸
+ (instancetype)navWithCustomView:(UIView *)customView
frame:(CGRect)frame
backAction:(void(^)(void))backAction
{
NavigationBar *nav = [[NavigationBar alloc] init];
if (customView) {
CGFloat x = backAction ? frame.origin.x + 44 : frame.origin.x;
customView.frame = Rect(x, frame.origin.y, frame.size.width, frame.size.height);
[nav addSubview:customView];
}
// 如果实现了返回的事件,才创建返回按钮
if (backAction) {
nav.backBlock = backAction;
UIButton *back = [UIButton buttonWithTitle:nil titleColor:nil backgroundColor:nil font:ButtonFont image:BackButtonImageName frame:Rect(0, HeightStatusBar, 44, 44)];
[back addTarget:nav action:@selector(backClick)];
back.adjustsImageWhenHighlighted = NO;
[nav addSubview:back];
}
return nav;
}
/// 自定义视图 customView
+ (instancetype)navWithCustomView:(UIView *)customView frame:(CGRect)frame
{
NavigationBar *nav = [[NavigationBar alloc] initWithCustomViewFrame:frame];
if (customView) {
//customView.frame = Rect(0, frame.origin.y, frame.size.width, frame.size.height);
[nav addSubview:customView];
}
return nav;
}
@end
static const char NavgationBarkey = '\0';
@implementation UIViewController (NavigatiionBar)
- (void)setRm_navgationBar:(NavigationBar *)rm_navgationBar
{
if (self.rm_navgationBar != rm_navgationBar)
{
[self.rm_navgationBar removeFromSuperview];
[self.view addSubview:rm_navgationBar];
objc_setAssociatedObject(self, &NavgationBarkey, rm_navgationBar, OBJC_ASSOCIATION_ASSIGN);
}
}
- (NavigationBar *)rm_navgationBar
{
return objc_getAssociatedObject(self, &NavgationBarkey);
}
@end
二哈.gif