iOS 加载NIB / xib 的 VIew

// .h

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

#define NIB(x)  (x *)[ViewUtil nib:#x]

#define NIB_OWN(x, y)  (x *)[ViewUtil nib:#x owner:y]

#define REG_NIB(x, y)  [ViewUtil table:x registerNib:#y]

#define CELL(x, y) (y *)[ViewUtil table:x nib:#y]

#define ScreenWidth ([UIScreen mainScreen].bounds.size.width)

#define Screenheight ([UIScreen mainScreen].bounds.size.height)

@interface ViewUtil :NSObject

+ (UIImage *)colorImage:(UIColor *)c frame:(CGRect)frame;

+ (UIView *)nib:(char *)nib;

+ (UIView *)nib:(char *)nib owner:(id)owner;

+ (UITableViewCell *)table:(UITableView *)table nib:(char *)nib;

+ (void)table:(UITableView *)table registerNib:(char *)nib;

@end


//  .m

#import"ViewUtil.h"

@implementation ViewUtil

+ (UIImage *)colorImage:(UIColor *)c frame:(CGRect)frame {

  staticNSMutableDictionary *imageCache;

  if (!imageCache) { imageCache = [[NSMutableDictionaryalloc] init];}

  

  CGFloat w = frame.size.width;

  CGFloat h = frame.size.height;

  

  NSString *cache_key = [NSStringstringWithFormat:@"%@_%d_%d",c, (int)w, (int)h];

  

  if (![imageCacheobjectForKey:cache_key]) {

    UIImage *img;

    CGRect rect=CGRectMake(0.0f,0.0f, w, h);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [cCGColor]);

    CGContextFillRect(context, rect);

    img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    imageCache[cache_key] = img;

  }

  return imageCache[cache_key];

}


+ (UIView *)nib:(char *)nib {

  NSArray *nibs=[[NSBundlemainBundle] loadNibNamed:[NSStringstringWithUTF8String:nib]

                                             owner:self

                                           options:nil];

  return [nibsobjectAtIndex:0];

}


+ (UIView *)nib:(char *)nib owner:(id)owner{

  NSArray *nibs=[[NSBundlemainBundle] loadNibNamed:[NSStringstringWithUTF8String:nib]

                                             owner:owner

                                           options:nil];

  return [nibsobjectAtIndex:0];

}


+ (UITableViewCell *)table:(UITableView *)table nib:(char *)nib {

  UITableViewCell *cell = (UITableViewCell *)[tabledequeueReusableCellWithIdentifier:[NSStringstringWithUTF8String:nib]];

  if (cell ==nil) {

    cell = (UITableViewCell *)[selfnib:nib];

  }

  return cell;

}


+ (void)table:(UITableView *)table registerNib:(char *)nib {

  [table registerNib:[UINibnibWithNibName:[NSStringstringWithUTF8String:nib]bundle:nil]forCellReuseIdentifier:[NSStringstringWithUTF8String:nib]];

}



//  外部调用

#import"testView.h"

#import "ViewUtil.h"

@interfaceViewController ()

@property (nonatomic,strong) testView *views;

@end


@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    _views = NIB(testView);

    _views.frame=CGRectMake(0,64, ScreenWidth-100,Screenheight-64);

    [self.viewaddSubview:_views];

}

@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值