runtime簡單使用-Empty-展示VC

P9h8發表於2018-09-07

#import <UIKit/UIKit.h>

@interface UIViewController (Empty)

  • (void)showEmptyWithTitle:(NSString *)title;
  • (void)hideEmptyView;
  • (void)showEmptyTitle:(NSString *)title;
  • (void)hideEmptyTitle;

@end

#import "UIViewController+Empty.h"

#import "CCEmptyView.h"

#import <objc/runtime.h>

static char emptyKey;

@implementation UIViewController (Empty)

  • (CCEmptyView *)emptyView{

  • CCEmptyView *emptyView = objc_getAssociatedObject(self, &emptyKey);

    if (emptyView) {

      return emptyView;
    複製程式碼

    }else{

      emptyView = [[CCEmptyView alloc] initWithFrame:self.view.bounds imageName:@"empty" title:nil];
      
      objc_setAssociatedObject(self, &emptyKey, emptyView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    複製程式碼

    }

    return emptyView;

}

  • (void)showEmptyWithTitle:(NSString *)title{

  • CCEmptyView *emptyView = [self emptyView];

    [self.view addSubview:emptyView];

    [emptyView setTitle:title];

    [emptyView showEmpty];

}

  • (void)hideEmptyView{
  • [[self emptyView] hideEmpty];

}

  • (void)showEmptyTitle:(NSString *)title{

  • UILabel *titleLabel = [self.view viewWithTag:111];

    if (!titleLabel) {

      titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 50)];
      
      titleLabel.textAlignment = NSTextAlignmentCenter;
      
      titleLabel.font = [UIFont systemFontOfSize:16];
      
      titleLabel.textColor =[UIColor redColor];
      
      titleLabel.tag = 111;
      
      titleLabel.numberOfLines = 0;
      
      titleLabel.center = self.view.center;
      
      [self.view addSubview:titleLabel];
    複製程式碼

    }

    titleLabel.hidden = NO;

    titleLabel.text = title;

}

  • (void)hideEmptyTitle{

  • UILabel *titleLabel = [self.view viewWithTag:111];

    titleLabel.numberOfLines = 0;

    titleLabel.hidden = YES;

}

呼叫方法(ps:使用2或3?你的需要啦~)

1:引入標頭檔案

2:[XX showEmptyWithTitle:@"掘金-Very good"];

3:[XX showEmptyTitle:@"掘金-Very good"];

相關文章