介紹
快捷為界面添加占位圖(無數(shù)據(jù)占位圖,網(wǎng)絡(luò)失敗占位圖等),可調(diào)整界面布局,以及動態(tài)更新界面。
初始化方法及使用
_emptyView = [XKEmptyPlaceView configScrollView:self.tableView config:nil];
[_emptyView showWithImgName:@"xxx" title:@"xxx" des:nil tapClick:^{ }];
可以使用屬性存儲XKEmptyPlaceView對象,配置后調(diào)用占位圖的方法顯示隱藏。為了方便使用,給UIScrollView加了一個分類,可以不用申明屬性,快速使用占位圖
快捷初始化方法及使用
// 初始化方法
[self.tableView configDefaultEmptyView];
[self.tableView.emptyPlaceView showWithImgName:@"xxx" title:@"xxxx" des:nil tapClick:^{ }];
使用范例
[HTTPClient postXXXX:^(id responseObject) {
self.datas = xxx;
if (self.datas.count) {
[self.emptyTipView hide];
} else {
[self.emptyTipView showWithImgName:kEmptyPlaceImgName title:nil des:@"暫無數(shù)據(jù)" tapClick:nil];
}
} failure:^(XKHttpErrror *error) {
if (self.datas.count == 0) {
__weak typeof(self) weakSelf = self;
self.emptyTipView.config.viewAllowTap = YES;
[self.emptyTipView showWithImgName:kNetErrorPlaceImgName title:@"網(wǎng)絡(luò)錯誤" des:@"點擊屏幕重試" tapClick:^{
[weakSelf request];
}];
}
}];
效果

image.png
圖片+標(biāo)題
__weak typeof(self) weakSelf = self;
_emptyView = [XKEmptyPlaceView configScrollView:self.tableView config:nil];
[_emptyView showWithImgName:@"暫無內(nèi)容" title:@"暫無數(shù)據(jù)" des:nil tapClick:^{
//
}];

無數(shù)據(jù)
圖片+標(biāo)題+描述
__weak typeof(self) weakSelf = self;
_emptyView = [XKEmptyPlaceView configScrollView:self.tableView config:nil];
[_emptyView showWithImgName:@"加載失敗" title:@"無法獲取網(wǎng)絡(luò)" des:@"點擊屏幕重試" tapClick:^{
[weakSelf.emptyView hide];
}];

無網(wǎng)絡(luò)
圖片+標(biāo)題+描述+按鈕
__weak typeof(self) weakSelf = self;
_emptyView = [XKEmptyPlaceView configScrollView:self.tableView config:nil];
_emptyView.config.viewAllowTap = NO;
[_emptyView showWithImgName:@"加載失敗" title:@"無法獲取網(wǎng)絡(luò)" des:@"請檢查網(wǎng)絡(luò)" btnText:@"重試" btnImg:nil tapClick:^{
[weakSelf.emptyView hide];
}];

image.png
多配置
- (void)viewDidLoad {
[super viewDidLoad];
__weak typeof(self) weakSelf = self;
XMEmptyViewConfig *config = [XMEmptyViewConfig new];
config.titleFont = [UIFont systemFontOfSize:18];
config.verticalOffset = -50;
config.btnBackImg = [UIColor orangeColor];
config.btnColor = [UIColor whiteColor];
config.btnFont = [UIFont systemFontOfSize:18];
config.btnMargin = 50;
config.spaceDesBtmHeight = 20;
config.spaceTitleBtmHeight = 30;
_emptyView = [XKEmptyPlaceView configScrollView:self.tableView config:config];
[self showNetError];
}
- (void)showNetError {
__weak typeof(self) weakSelf = self;
// 展示前可繼續(xù)動態(tài)配置config
self.emptyView.config.viewAllowTap = NO;
_emptyView.config.titleColor = [UIColor blackColor];
[_emptyView showWithImgName:@"404出錯頁面" title:@"不開心\n無法獲取網(wǎng)絡(luò)" des:@"檢查一下網(wǎng)絡(luò)" btnText:@"重試" btnImg:nil tapClick:^{
[weakSelf showEmpty];
}];
}
- (void)showEmpty {
__weak typeof(self) weakSelf = self;
_emptyView.config.viewAllowTap = YES;
_emptyView.config.titleColor = [UIColor orangeColor];
[_emptyView showWithImgName:@"暫無內(nèi)容" title:@"暫無數(shù)據(jù)" des:@"戳屏幕再刷新" tapClick:^{
[weakSelf showNetError];
}];
}

image.png
使用要點
1. 目前只能用于UIScrollView, 如果普通界面需要使用,可先創(chuàng)建UIScrollView進(jìn)行承載。
2. config配置是核心,是對界面的顯示描述,在show方法調(diào)用時,會使用config的配置進(jìn)行界面的重繪。
3. emptyView自帶有config屬性,初始化未為傳入會自動生成一個默認(rèn)的config,針對自己的app設(shè)計,可以將基礎(chǔ)的配置項在默認(rèn)配置里設(shè)置完,外界只配置特殊的。 在展示前可獲取congfig進(jìn)行屬性配置,即可繼續(xù)動態(tài)配置占位圖的顯示效果。 config的界面配置屬性如果設(shè)置過多,顯示其他界面不需要時,可重置,設(shè)置可調(diào)用resetConfig
4. 封裝的顯示方法里考慮了方便動態(tài)更新界面,所以將會動態(tài)變化頻繁的參數(shù)放入了方法中,其實內(nèi)部也是對config進(jìn)行的賦值,在show方法之前單獨設(shè)置的img,title,des會被方法傳入的覆蓋:
/**不帶按鈕的占位圖*/
- (void)showWithImgName:(NSString *)imgName title:(NSString *)title des:(NSString *)des tapClick:(void(^)(void))tap;
/**下方帶按鈕的 btnImg 可是image or 圖片名稱*/
- (void)showWithImgName:(NSString *)imgName title:(NSString *)title des:(NSString *)des btnText:(NSString *)btnText btnImg:(id)btnImg tapClick:(void(^)(void))tap;
需求場景可為界面先為網(wǎng)絡(luò)錯誤占位圖,后請求成功為無數(shù)據(jù)占位??蓞⒖?code>多配置demo
5. 視圖的點擊事件和按鈕的點擊回調(diào)是同一個,當(dāng)開啟config.viewAllowTap = YES;,點擊屏幕和按鈕都會回調(diào)block,如果只想按鈕點擊,設(shè)置為NO即可
Installation
XKEmptyPlaceView is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod 'XKEmptyPlaceView'
Author
Jamesholy, 447523382@qq.com