1.一般UIView 创建
UIView *cellView = [[UIView alloc] init];
[superView addSubview:cellView];
cellView.layer.cornerRadius = 25*ViewW;
cellView.backgroundColor = RGB(255,255,255,0.28);
[cellView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(320*ViewW, 50*ViewW));
make.centerX.mas_equalTo(superView.mas_centerX);
make.top.mas_equalTo(topView.mas_top).mas_offset(topS);
}];
2.自定义UIView 创建.m文件
#import "Custom.h"
@interface Custom()
{
}
@end
@implementation Custom
//初始化
-(id)initWithDelegate:(id<CustomDelegate>)delegate SuperView:(UIView *)superView{
self = [super initWithFrame:superView.frame];
if (self) {
_delegate = delegate;
[superView addSubview:self];
//初始化代码
[self creatSubViews];
}
return self;
}
//创建子视图
-(void)creatSubViews{
}
.h文件
//
// CustomView.h
//
//
// Created by 周家稳 on 2018/4/26.
// Copyright © 2018年 zhoujiawen. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol CustomViewDelegate <NSObject> // 代理传值方法
@end
@interface CustomView : UIView
@property (weak, nonatomic) id<CustomViewDelegate> delegate;
//初始化
-(id)initWithDelegate:(id<CustomViewDelegate>)delegate SuperView:(UIView *)superView;
@end
在引用的父视图的.m文件
//
//
// Created by 周家稳 on 2018/4/26.
// Copyright © 2018年 zhoujiawen. All rights reserved.
//
#import "CustomViewController"
@interface CustomViewController ()<CustomViewDelegate>
{
CustomView * Custom;
}
@end
@implementation CustomViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
Custom = [[CustomView alloc] initWithDelegate:self SuperView:self.view];
}