欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-08-21 17:59:35
ios学习第五天(二)给页面中的view添加子view,上一篇文章已经对ios页面进行了说明,接下来就是使用页面,给页面中添加子控件,在上面两篇创建的项目中,进行些许修改,成了现在的。先看效果: 效...

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

效果:

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

代码:

修改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