ios学习第五天(二)给页面中的View添加子View

这篇博客介绍了如何在iOS开发中为页面的View添加子View,通过展示实际效果和提供修改ViewController的代码来详细讲解操作过程。

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

上一篇文章已经对ios页面进行了说明,接下来就是使用页面,给页面中添加子控件,在上面两篇创建的项目中,进行些许修改,成了现在的。先看效果:

效果:



代码:

修改ViewController

//
//  ViewController.m
//  HelloIOS
//
//  Created by Moluth on 17/4/10.
//  Copyright (c) 2017年 Moluth. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[[UIColor alloc] initWithRed:0.3f green:0.35f blue:0.85f alpha:1.0f];
    
    UIView *view1=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]//创建view ,view是矩形,0,0表示左上角坐标,后面的100,100代表宽高
    view1.backgroundColor=[[UIColor alloc] initWithRed:0.88f green:0.66f blue:0.11f alpha:0.8f];//给新创建的view设置背景颜色,便于观察
    [self.view addSubview:view1];//向ViewController中的view中添加子View
    
    //下面代码都是对上面的复制和修改,仅仅是坐标和颜色不同
    UIView *view2=[[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
    view2.backgroundColor=[[UIColor alloc] initWithRed:1.0f green:0.0f blue:0.6f alpha:0.8f];
    [self.view addSubview:view2];
    
    UIView *view3=[[UIView alloc] initWithFrame:CGRectMake(60, 60, 100, 100)];
    view3.backgroundColor=[[UIColor alloc] initWithRed:1.0f green:1.0f blue:0.6f alpha:0.8f];
    [self.view addSubview:view3];
    
    UIView *view4=[[UIView alloc] initWithFrame:CGRectMake(90, 90, 100, 100)];
    view4.backgroundColor=[[UIColor alloc] initWithRed:0.0f green:1.0f blue:0.0f alpha:0.8f];
    [self.view addSubview:view4];
    
    
    //self.view
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值