1
This commit is contained in:
@@ -40,6 +40,22 @@ typedef void(^KBEmptyAction)(void);
|
||||
/// 触发刷新空数据视图(若集成了 DZNEmptyDataSet 则调用其 reloadEmptyDataSet)
|
||||
- (void)kb_reloadEmptyDataSet;
|
||||
|
||||
/// 使用 LYEmptyView 在表格页快速挂载一套统一样式的空态视图(与 DZNEmptyDataSet 二选一)
|
||||
/// 注意:调用该方法会自动关闭本类内置的 DZNEmptyDataSet(useEmptyDataSet = NO),避免冲突。
|
||||
/// - Parameters:
|
||||
/// - image: 占位图(可空)
|
||||
/// - title: 标题(默认“暂无数据”)
|
||||
/// - detail: 描述(可空)
|
||||
/// - buttonTitle: 按钮标题(可空,传空则不展示按钮)
|
||||
/// - tapHandler: 点击空白区域回调(可空)
|
||||
/// - buttonHandler: 按钮点击回调(可空)
|
||||
- (void)kb_makeDefaultEmptyViewWithImage:(nullable UIImage *)image
|
||||
title:(nullable NSString *)title
|
||||
detail:(nullable NSString *)detail
|
||||
buttonTitle:(nullable NSString *)buttonTitle
|
||||
tapHandler:(nullable KBEmptyAction)tapHandler
|
||||
buttonHandler:(nullable KBEmptyAction)buttonHandler;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
@@ -13,6 +13,15 @@
|
||||
#define KB_HAS_DZN 0
|
||||
#endif
|
||||
|
||||
// 可选引入:LYEmptyView(通过 Pod 集成),用于空态视图的“自动显隐+属性配置”方案
|
||||
#if __has_include(<LYEmptyView/UIView+Empty.h>)
|
||||
#import <LYEmptyView/UIView+Empty.h>
|
||||
#import <LYEmptyView/LYEmptyView.h>
|
||||
#define KB_HAS_LY 1
|
||||
#else
|
||||
#define KB_HAS_LY 0
|
||||
#endif
|
||||
|
||||
@interface BaseTableView ()
|
||||
#if KB_HAS_DZN
|
||||
<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
|
||||
@@ -127,4 +136,66 @@
|
||||
|
||||
#endif
|
||||
|
||||
// MARK: - LYEmptyView 快速挂载(与 DZN 二选一)
|
||||
- (void)kb_makeDefaultEmptyViewWithImage:(UIImage *)image
|
||||
title:(NSString *)title
|
||||
detail:(NSString *)detail
|
||||
buttonTitle:(NSString *)buttonTitle
|
||||
tapHandler:(KBEmptyAction)tapHandler
|
||||
buttonHandler:(KBEmptyAction)buttonHandler {
|
||||
#if KB_HAS_LY
|
||||
// 关闭 DZN,避免两套空态叠加
|
||||
self.useEmptyDataSet = NO;
|
||||
|
||||
// 默认文案
|
||||
NSString *t = title ?: @"暂无数据";
|
||||
|
||||
LYEmptyView *ev = nil;
|
||||
if (buttonTitle.length > 0) {
|
||||
// 带按钮(优先 block 方式)
|
||||
__weak typeof(self) weakSelf = self;
|
||||
ev = [LYEmptyView emptyActionViewWithImage:image
|
||||
titleStr:t
|
||||
detailStr:detail ?: @""
|
||||
btnTitleStr:buttonTitle
|
||||
btnClickBlock:^{ if (buttonHandler) buttonHandler(); [weakSelf setNeedsLayout]; }];
|
||||
} else {
|
||||
// 无按钮
|
||||
ev = [LYEmptyView emptyViewWithImage:image titleStr:t detailStr:detail ?: @""];
|
||||
}
|
||||
|
||||
// 点击空白区域
|
||||
if (tapHandler) {
|
||||
ev.tapEmptyViewBlock = ^{ tapHandler(); };
|
||||
}
|
||||
|
||||
// 统一样式(可按需微调)
|
||||
ev.emptyViewIsCompleteCoverSuperView = NO; // 不全屏覆盖,按内容大小布局
|
||||
ev.ignoreContentInset = YES; // 忽略 inset,避免导航/安全区影响
|
||||
ev.contentViewOffset = -10; // 轻微上移
|
||||
ev.subViewMargin = 14; // 子控件间距
|
||||
ev.titleLabFont = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
|
||||
ev.titleLabTextColor = [UIColor colorWithWhite:0.45 alpha:1.0];
|
||||
ev.detailLabFont = [UIFont systemFontOfSize:14];
|
||||
ev.detailLabTextColor = [UIColor colorWithWhite:0.6 alpha:1.0];
|
||||
ev.detailLabLineSpacing = 3;
|
||||
ev.detailLabMaxLines = 2;
|
||||
ev.actionBtnFont = [UIFont systemFontOfSize:15 weight:UIFontWeightSemibold];
|
||||
ev.actionBtnCornerRadius = 6;
|
||||
ev.actionBtnBorderWidth = 1;
|
||||
ev.actionBtnBorderColor = [UIColor colorWithWhite:0.88 alpha:1.0];
|
||||
ev.actionBtnTitleColor = [UIColor colorWithRed:0.22 green:0.49 blue:0.96 alpha:1.0];
|
||||
ev.actionBtnBackGroundColor = [UIColor whiteColor];
|
||||
ev.actionBtnHorizontalMargin = 20; // 自动宽度:文字 + padding*2
|
||||
|
||||
// 自动显隐交由控制层(网络请求)
|
||||
ev.autoShowEmptyView = NO;
|
||||
|
||||
self.ly_emptyView = (LYEmptyView *)ev;
|
||||
#else
|
||||
// 未集成 LYEmptyView 时不做任何处理,保持兼容
|
||||
(void)image; (void)title; (void)detail; (void)buttonTitle; (void)tapHandler; (void)buttonHandler;
|
||||
#endif
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
40
keyBoard/Class/Base/V/UIScrollView+KBEmptyView.h
Normal file
40
keyBoard/Class/Base/V/UIScrollView+KBEmptyView.h
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// UIScrollView+KBEmptyView.h
|
||||
// keyBoard
|
||||
//
|
||||
// 统一封装基于 LYEmptyView 的空态视图挂载方法,适用于 UITableView/UICollectionView。
|
||||
// 注意:仅在对应页面已通过 CocoaPods 集成 LYEmptyView 时生效。
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
typedef void(^KBEmptyAction)(void);
|
||||
|
||||
@interface UIScrollView (KBEmptyView)
|
||||
|
||||
/// 快速挂载一套统一样式的空态视图(LYEmptyView)
|
||||
/// - Parameters:
|
||||
/// - image: 占位图(可空)
|
||||
/// - title: 标题(默认“暂无数据”)
|
||||
/// - detail: 描述(可空)
|
||||
/// - buttonTitle: 按钮标题(可空,传空则不显示按钮)
|
||||
/// - tapHandler: 点击空白区域回调(可空)
|
||||
/// - buttonHandler: 按钮点击回调(可空)
|
||||
- (void)kb_makeDefaultEmptyViewWithImage:(nullable UIImage *)image
|
||||
title:(nullable NSString *)title
|
||||
detail:(nullable NSString *)detail
|
||||
buttonTitle:(nullable NSString *)buttonTitle
|
||||
tapHandler:(nullable KBEmptyAction)tapHandler
|
||||
buttonHandler:(nullable KBEmptyAction)buttonHandler;
|
||||
|
||||
/// 便捷加载态控制:关闭自动显隐后,配合网络请求手动控制显示/隐藏
|
||||
- (void)kb_setLYAutoShowEnabled:(BOOL)enabled; // YES=自动显隐;NO=手动
|
||||
- (void)kb_beginLoadingForEmpty; // 请求开始:临时隐藏 empty
|
||||
- (void)kb_endLoadingForEmpty; // 请求结束:根据数据源显示/隐藏(需确保先 reloadData)
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
90
keyBoard/Class/Base/V/UIScrollView+KBEmptyView.m
Normal file
90
keyBoard/Class/Base/V/UIScrollView+KBEmptyView.m
Normal file
@@ -0,0 +1,90 @@
|
||||
//
|
||||
// UIScrollView+KBEmptyView.m
|
||||
// keyBoard
|
||||
//
|
||||
|
||||
#import "UIScrollView+KBEmptyView.h"
|
||||
|
||||
#if __has_include(<LYEmptyView/UIView+Empty.h>)
|
||||
#import <LYEmptyView/UIView+Empty.h>
|
||||
#import <LYEmptyView/LYEmptyView.h>
|
||||
#define KB_HAS_LY 1
|
||||
#else
|
||||
#define KB_HAS_LY 0
|
||||
#endif
|
||||
|
||||
@implementation UIScrollView (KBEmptyView)
|
||||
|
||||
- (void)kb_makeDefaultEmptyViewWithImage:(UIImage *)image
|
||||
title:(NSString *)title
|
||||
detail:(NSString *)detail
|
||||
buttonTitle:(NSString *)buttonTitle
|
||||
tapHandler:(KBEmptyAction)tapHandler
|
||||
buttonHandler:(KBEmptyAction)buttonHandler {
|
||||
#if KB_HAS_LY
|
||||
NSString *t = title ?: @"暂无数据";
|
||||
LYEmptyView *ev = nil;
|
||||
|
||||
if (buttonTitle.length > 0) {
|
||||
__weak typeof(self) weakSelf = self;
|
||||
ev = [LYEmptyView emptyActionViewWithImage:image
|
||||
titleStr:t
|
||||
detailStr:detail ?: @""
|
||||
btnTitleStr:buttonTitle
|
||||
btnClickBlock:^{ if (buttonHandler) buttonHandler(); [weakSelf setNeedsLayout]; }];
|
||||
} else {
|
||||
ev = [LYEmptyView emptyViewWithImage:image titleStr:t detailStr:detail ?: @""];
|
||||
}
|
||||
|
||||
if (tapHandler) { ev.tapEmptyViewBlock = ^{ tapHandler(); }; }
|
||||
|
||||
// 统一样式
|
||||
ev.emptyViewIsCompleteCoverSuperView = NO;
|
||||
ev.ignoreContentInset = YES;
|
||||
ev.contentViewOffset = -10;
|
||||
ev.subViewMargin = 14;
|
||||
ev.titleLabFont = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
|
||||
ev.titleLabTextColor = [UIColor colorWithWhite:0.45 alpha:1.0];
|
||||
ev.detailLabFont = [UIFont systemFontOfSize:14];
|
||||
ev.detailLabTextColor = [UIColor colorWithWhite:0.6 alpha:1.0];
|
||||
ev.detailLabLineSpacing = 3;
|
||||
ev.detailLabMaxLines = 2;
|
||||
ev.actionBtnFont = [UIFont systemFontOfSize:15 weight:UIFontWeightSemibold];
|
||||
ev.actionBtnCornerRadius = 6;
|
||||
ev.actionBtnBorderWidth = 1;
|
||||
ev.actionBtnBorderColor = [UIColor colorWithWhite:0.88 alpha:1.0];
|
||||
ev.actionBtnTitleColor = [UIColor colorWithRed:0.22 green:0.49 blue:0.96 alpha:1.0];
|
||||
ev.actionBtnBackGroundColor = [UIColor whiteColor];
|
||||
ev.actionBtnHorizontalMargin = 20;
|
||||
|
||||
// 默认关闭自动显隐,交给调用方用 begin/end 控制
|
||||
ev.autoShowEmptyView = NO;
|
||||
|
||||
self.ly_emptyView = ev;
|
||||
#else
|
||||
(void)image; (void)title; (void)detail; (void)buttonTitle; (void)tapHandler; (void)buttonHandler;
|
||||
#endif
|
||||
}
|
||||
|
||||
- (void)kb_setLYAutoShowEnabled:(BOOL)enabled {
|
||||
#if KB_HAS_LY
|
||||
self.ly_emptyView.autoShowEmptyView = enabled;
|
||||
#else
|
||||
(void)enabled;
|
||||
#endif
|
||||
}
|
||||
|
||||
- (void)kb_beginLoadingForEmpty {
|
||||
#if KB_HAS_LY
|
||||
[self ly_startLoading];
|
||||
#endif
|
||||
}
|
||||
|
||||
- (void)kb_endLoadingForEmpty {
|
||||
#if KB_HAS_LY
|
||||
[self ly_endLoading];
|
||||
#endif
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user