自定义带xib的view

@interface YuanView : UIView

@property (weak, nonatomic) IBOutlet UIButton *button;

+ (YuanView *)createCustomView;

@end

#import "YuanView.h"


@implementation YuanView

//创建view初始化的两种方式

- (instancetype)init {

    

    if (self = [super init]) {

        self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([YuanView class]) owner:self options:nil] lastObject];

    }

    return self;

}

+ (YuanView *)createCustomView{

    return [[[NSBundle mainBundle] loadNibNamed:@"YuanView" owner:nil options:nil]lastObject];

}

@end

//加载view的两种方式 在控制器中可以这样写


//    YuanView *yuanView = [[YuanView alloc]init];

//    

//    yuanView.frame = self.view.frame;

//

//    [self.view addSubview:yuanView];


    YuanView *yuanCuVi = [YuanView createCustomView];//类方法调用

    yuanCuVi.frame self.view.frame;

    [self.view addSubview:yuanCuVi];


猜你喜欢

转载自blog.csdn.net/chungeshihuatian/article/details/50010761