任何在屏幕显示的图形都是矩形,只是做了处理。因为所有在iOS能看到都是UIView或UIView的子类。
//
// ViewController.m
// UIView01
//
// Created by cqy on 16/2/12.
// Copyright © 2016年 程清杨. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建View对象
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 200, 100)];
//设置背景颜色
view1.backgroundColor = [UIColor yellowColor];
//添加视图
[self.view addSubview:view1];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// ViewController.m
// UIView01
//
// Created by cqy on 16/2/12.
// Copyright © 2016年 程清杨. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建View对象
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 200, 100)];
//设置背景颜色
view1.backgroundColor = [UIColor yellowColor];
//添加视图
[self.view addSubview:view1];
// 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
注释:
1、CGRectMake是个返回结构体有四个参数来确定我们所创建视图的位置和大小;
2、CGRect是⼀个结构体,有两个成员变CGPoint、CGSize,这两个成员变量都是结构体,每个成员变量⾥都分别有两个成员变量,都是CGFloat类型的;
3、CGPoint的两个成员变量是来确定我们创建视图的位置;
4、CGSize的两个成员变量是来确定我们创建视图的大小;
5、起始位置:从屏幕的最左上⾓为(0,0)点,往下为正Y轴,右为正X轴;
6、addSubview,把我们创建的视图,添加到view中,这样,才能使我们的视图可⻅。