美文网首页
UIScrollView类别

UIScrollView类别

作者: 猪队友小L | 来源:发表于2017-08-19 11:21 被阅读10次
#import <UIKit/UIKit.h>

@interface UIScrollView (Extension_1)

- (void)showNodataViewTopText:(NSString *)text;

- (void)showNodataViewTopText:(NSString *)text high:(float)high;

- (void)showNoDataViewTopText:(NSString *)text image:(UIImage *)image;

- (void)showNoDataViewTopText:(NSString *)text extentText:(NSArray *)extendTexts image:(UIImage *)image;

- (void)hiddenNodataView;

@end

#import "UIScrollView+Extension_1.h"
#import <objc/runtime.h>

@interface UIScrollView ()

@property (weak, nonatomic ,readwrite) UIView *nodataView;
@property (weak, nonatomic, readwrite) UIView *noMessageView;

@end

@implementation UIScrollView (Extension_1)
//无数据显示
- (void)showNodataViewTopText:(NSString *)text
{
    [self.nodataView removeFromSuperview];
    self.nodataView = nil;
    
    //暂无搜索结果
    UILabel *textLabel = [UILabel labelWithFrame:CGRectZero text:text textColor:colorForDeepGrayNormal textAlignment:NSTextAlignmentCenter font:Arial(14)];
    [textLabel sizeToFit];
    [textLabel setFrame:CGRectMake(0, 0, kScreenWidth, textLabel.height)];
    
    self.nodataView = textLabel;
}

//无数据显示
- (void)showNodataViewTopText:(NSString *)text high:(float)high
{
    [self.nodataView removeFromSuperview];
    self.nodataView = nil;
    
    //暂无搜索结果
    UILabel *textLabel = [UILabel labelWithFrame:CGRectZero text:text textColor:colorForDeepGrayNormal textAlignment:NSTextAlignmentCenter font:Arial(14)];
    [textLabel sizeToFit];
    [textLabel setFrame:CGRectMake(0, high, kScreenWidth, textLabel.height)];
    self.nodataView = textLabel;
}

- (void)showNoDataViewTopText:(NSString *)text image:(UIImage *)image
{
    [self.nodataView removeFromSuperview];
    self.nodataView = nil;
    
    UIView *nodataView = [[UIView alloc] initWithFrame:CGRectMake(0,100, kScreenWidth,0)];
    
    UIImage *nodataImg = IsNilOrNull(image)?[UIImage imageNamed:@"common_nodata"]:image;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth-nodataImg.size.width)/2,0, nodataImg.size.width,nodataImg.size.height)];
    imageView.image = nodataImg;
    [nodataView addSubview:imageView];
    
    UILabel *titleLbl =[UILabel labelWithFrame:CGRectZero text:text textColor:colorForDeepGrayNormal textAlignment:NSTextAlignmentCenter font:Arial(14)];
    titleLbl.numberOfLines = 0;
    [titleLbl setFrame:CGRectMake(0, imageView.bottom+10, kScreenWidth, 32)];
    [nodataView addSubview:titleLbl];
    
//    titleLbl.backgroundColor =[UIColor redColor];
    
    nodataView.height = titleLbl.bottom + 20;
    
    self.nodataView = nodataView;
}

- (void)showNoDataViewTopText:(NSString *)text extentText:(NSArray *)extendTexts image:(UIImage *)image
{
    [self.nodataView removeFromSuperview];
    self.nodataView = nil;
    
    UIView *nodataView = [[UIView alloc] initWithFrame:CGRectMake(0,100, kScreenWidth,0)];
    
    UIImage *nodataImg = IsNilOrNull(image)?[UIImage imageNamed:@"common_nodata"]:image;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth-nodataImg.size.width)/2,0, nodataImg.size.width,nodataImg.size.height)];
    imageView.image = nodataImg;
    [nodataView addSubview:imageView];
    
    UILabel *titleLbl =[UILabel labelWithFrame:CGRectZero text:text textColor:colorForDeepGrayNormal textAlignment:NSTextAlignmentCenter font:Arial(14)];
    [titleLbl sizeToFit];
    [titleLbl setFrame:CGRectMake(0, imageView.bottom+10, kScreenWidth, titleLbl.height)];
    [nodataView addSubview:titleLbl];
    
    CGFloat yPoint = titleLbl.bottom + 10;
    
    for(NSString *str in extendTexts){
        
        UILabel *textLbl =[UILabel labelWithFrame:CGRectZero text:str textColor:colorForLightGrayNormal textAlignment:NSTextAlignmentCenter font:Arial(12)];
        [textLbl sizeToFit];
        [textLbl setFrame:CGRectMake(0, yPoint, kScreenWidth, textLbl.height)];
        [nodataView addSubview:textLbl];
        
        yPoint = textLbl.bottom + 6;
    }
    
    nodataView.height = yPoint + 20;
    
    self.nodataView = nodataView;
}

- (void)hiddenNodataView
{
    [self.nodataView removeFromSuperview];
    self.nodataView = nil;
}

static char CNodataViewKey;
- (UIView *)nodataView
{
    return objc_getAssociatedObject(self, &CNodataViewKey);
}

- (void )setNodataView:(UIView *)nodataView
{
    [self willChangeValueForKey:@"nodataView"];
    objc_setAssociatedObject(self, &CNodataViewKey,
                             nodataView,
                             OBJC_ASSOCIATION_ASSIGN);
    [self didChangeValueForKey:@"nodataView"];
    
    //    [self addSubview:nodataView];
    [self insertSubview:nodataView atIndex:0];
}
@end

相关文章

网友评论

      本文标题:UIScrollView类别

      本文链接:https://www.haomeiwen.com/subject/lhktdxtx.html