ios學習第五天(二)給頁面中的View添加子View,上一篇文章已經對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