iOS無數(shù)據(jù),無網(wǎng)絡(luò)占位圖封裝

介紹

快捷為界面添加占位圖(無數(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

Deme:https://github.com/sy5075391/XKEmptyPlaceView

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,097評論 0 23
  • 過年了,收到很多祝福,也發(fā)了很多祝福,彼此問候相互近況,有人歡喜有人憂! 有些日子不見: 有人做生意,辛苦一年沒賺...
    微笑思甜閱讀 297評論 0 0
  • “人易沉醉于幻象,而真實往往牽扯萬千,背對現(xiàn)實面,虛幻卻成了突破口?!?星期一早晨,陽光熹微。 耒迷迷糊糊地從床上...
    鐘裊閱讀 403評論 0 0
  • 本來打算寫完作業(yè)再去洗澡睡覺的,可剛剛實在太困了就趴在床上小睡了一會兒,這一睡竟然進(jìn)入了夢中夢的世界,花了好大的力...
    蘇姍走北交閱讀 482評論 2 1

友情鏈接更多精彩內(nèi)容