iOS OC的基本视图创建-UIView

本文详细介绍了在iOS开发中如何创建基本的UIView。通过查看.h文件和在父视图.m文件中的引用,理解UIView的实例化过程,是iOS界面构建的基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值