關(guān)鍵詞:空白頁面、無網(wǎng)絡(luò)訪問權(quán)限囤萤、沒有網(wǎng)絡(luò)、DZNEmptyDataSet、跳轉(zhuǎn)到設(shè)置頁面
前言:空白頁面展示
現(xiàn)在的APP數(shù)據(jù)都是從網(wǎng)絡(luò)里獲取松靡,如果沒有設(shè)置特別的空白頁面提示,整個APP的使用性會差很多建椰,而且也會顯得很low
一雕欺、空白頁面通常的類型
- 沒有網(wǎng)絡(luò)訪問權(quán)限
第一次安裝APP后在提示允許使用網(wǎng)絡(luò)時點(diǎn)擊了否,APP沒有請求網(wǎng)絡(luò)的權(quán)限棉姐,這時沒有任何數(shù)據(jù)屠列,
應(yīng)該顯示【圖片+描述文字(如:APP沒有網(wǎng)絡(luò)訪問權(quán)限)+按鈕(如:名稱為‘去設(shè)置’,點(diǎn)擊去設(shè)置的這個按鈕跳轉(zhuǎn)到去打開網(wǎng)絡(luò)權(quán)限的設(shè)置頁面)
- 手機(jī)沒有網(wǎng)絡(luò)
有網(wǎng)絡(luò)訪問權(quán)限伞矩,但手機(jī)沒有網(wǎng)絡(luò)笛洛,既沒有開WiFi,也沒有開啟移動蜂窩網(wǎng)絡(luò)扭吁,通常情況是開了飛行模式或者在既沒有WiFi撞蜂、有沒有3g、4g的地鐵里或野外
應(yīng)該顯示【圖片+描述文字(如:網(wǎng)絡(luò)已經(jīng)斷開連接)+按鈕(如:名稱為‘設(shè)置網(wǎng)絡(luò)’侥袜,點(diǎn)擊設(shè)置網(wǎng)絡(luò)的這個按鈕跳轉(zhuǎn)到去開啟網(wǎng)絡(luò)的設(shè)置頁面)
- 當(dāng)前頁面的業(yè)務(wù)數(shù)據(jù)暫時沒有
網(wǎng)絡(luò)能正常請求蝌诡,但頁面確實沒有數(shù)據(jù),如剛開始使用APP時枫吧,很多頁面都沒有數(shù)據(jù)
應(yīng)該顯示【圖片+描述文字(如:暫無記錄)+按鈕(如:名稱為‘刷新’浦旱,點(diǎn)擊刷新的這個按鈕刷新數(shù)據(jù))
- 能網(wǎng)絡(luò)請求,但是網(wǎng)絡(luò)請求出錯了九杂,比如后端接口報500內(nèi)部錯誤等情況
. 在1颁湖、2兩種場景下,如果用戶點(diǎn)擊去開啟好了相關(guān)設(shè)置例隆,設(shè)置好從新回到APP里時甥捺,相關(guān)提示應(yīng)該有更新變化,如去設(shè)置了允許訪問網(wǎng)絡(luò)后镀层,當(dāng)用戶回到APP里時看到的提示語就應(yīng)該變成2或3里的內(nèi)容
二镰禾、使用runtime在不動DZNEmptyDataSet的源碼的情況下,修改空白頁面按鈕根據(jù)文字寬度自適應(yīng)水平居中展示不不是默認(rèn)的固定長寬度展示
#import <UIKit/UIKit.h>
@interface UIView (LHAutoWidthBtn)
@end
#import "UIView+LHAutoWidthBtn.h"
#import <objc/runtime.h>
@implementation UIView (LHAutoWidthBtn)
+ (void)load
{
Class clz = NSClassFromString(@"DZNEmptyDataSetView");
Method originalMethod1 = class_getInstanceMethod(clz, NSSelectorFromString(@"setupConstraints"));
Method swizzledMethod1 = class_getInstanceMethod(clz, @selector(lh_setupConstraints));
method_exchangeImplementations(originalMethod1, swizzledMethod1);
}
- (void)lh_setupConstraints
{
[self lh_setupConstraints];
UIButton *btn = [self valueForKeyPath:@"button"];
if (btn != nil && btn.superview != nil) {
UIView *superView = btn.superview;
NSDictionary *views = @{@"button":btn};
NSString *title = btn.titleLabel.text;
CGFloat btnWith = [self sizeWithText:title font:btn.titleLabel.font withHeight:20].width+10;
// 為了統(tǒng)一好看唱逢,如果小于100的都設(shè)置為100
if (btnWith<100) {
btnWith = 100;
}
// 最大寬度吴侦,由于限制了兩邊的padding,所以這里限制的最大寬度在小于width-2*padding時就是實際的寬度坞古,如果在計算的寬度大于這個寬度時备韧,則是width-2*padding的寬度
NSString *format = [NSString stringWithFormat:@"[button(<=%f)]",btnWith];
[superView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: format options:0 metrics:views views:views]];
//水平居中
NSLayoutConstraint *layout = [NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
[superView addConstraint:layout];
}
}
- (CGSize)sizeWithText:(NSString*)text font:(UIFont*)font withHeight:(float)maxHeight {
CGSize maxSize = CGSizeMake(CGFLOAT_MAX,maxHeight);
CGSize textSize = CGSizeZero;
// iOS7以后使用boundingRectWithSize,之前使用sizeWithFont
if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
// 多行必需使用NSStringDrawingUsesLineFragmentOrigin痪枫,網(wǎng)上有人說不是用NSStringDrawingUsesFontLeading計算結(jié)果不對
NSStringDrawingOptions opts = NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading;
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineBreakMode:NSLineBreakByCharWrapping];
NSDictionary *attributes = @{ NSFontAttributeName : font, NSParagraphStyleAttributeName : style };
CGRect rect = [text boundingRectWithSize:maxSize
options:opts
attributes:attributes
context:nil];
textSize = rect.size;
}
else{
textSize = [text sizeWithFont:font constrainedToSize:maxSize lineBreakMode:NSLineBreakByCharWrapping];
}
return textSize;
}
@end
三织堂、具體代碼實現(xiàn)