UIView表示屏幕上de一块矩形区域。负责渲染区域内的内容,并响应该区域内发生的触摸事件。
IOS坐标系:
屏幕最左到最右可划分为320等份。
屏幕最上到最下可划分为480等份。(3.5寸)
屏幕划分依据不是像素,而是点。
状态栏占20
视图的重要属性
属性名
|
描述
|
示例
|
hidden
|
控制视图的显隐
|
redView.hidden=YES; 隐藏redView
redView.hidden=NO;显示 redView
|
alpha
|
控制视图的不透明度(子视图也一起透明)取值范围0~1。
|
redView.alpha=0.9
|
superview
|
获取本视图的父视图
|
UIView *superVIew=[redView superView];
|
superVIews
|
获取本视图的所有子视图
|
UIView *superVIews=[redView superViews];
|
tag
|
给视图添加标记,被加完标记的视图可以使用viewWithTag:方法取出
|
redView.tag=100;
UIView *view=[superview viewWithTag:100];
|
#import
"AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建window ,让其充满屏幕
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//让window成为主窗口且可视
[self.window makeKeyAndVisible];
//设置背景色
self.window.backgroundColor = [UIColor whiteColor];
//设置根视图控制器
self.window.rootViewController = [[ViewController alloc]init];
return YES;
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建window ,让其充满屏幕
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//让window成为主窗口且可视
[self.window makeKeyAndVisible];
//设置背景色
self.window.backgroundColor = [UIColor whiteColor];
//设置根视图控制器
self.window.rootViewController = [[ViewController alloc]init];
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIView *view = [[UIView alloc]init];
//相对于父视图的位置,注意坐标和尺寸的合理性,保证坐标加尺寸不会超出父视图范围
view.frame=CGRectMake(10, 200, 100, 100);
//是否允许用户点击(默认YES),如果设置成NO,子视图不会覆盖父视图的点击事件。
view.userInteractionEnabled = YES;
view.backgroundColor = [UIColor orangeColor];
//将后边的视图添加到前面的视图之上
[self.view addSubview:view];//
// //如果父视图不允许交互,那么字视图的事件也会被屏蔽
// self.view.userInteractionEnabled =NO;
//设置视图的标签
view.tag=1;
//设置透明度,0~1浮点
view.alpha=0.8;
// //如果父视图透明,那么子视图也会看不见。
// self.view.alpha=0;
//设置视图是否隐藏(默认NO)
view.hidden=NO;
// //父视图被隐藏,那么子视图也会被隐藏
// self.view.hidden=YES;
//获取父视图中标签为1的视图
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIView *view = [[UIView alloc]init];
//相对于父视图的位置,注意坐标和尺寸的合理性,保证坐标加尺寸不会超出父视图范围
view.frame=CGRectMake(10, 200, 100, 100);
//是否允许用户点击(默认YES),如果设置成NO,子视图不会覆盖父视图的点击事件。
view.userInteractionEnabled = YES;
view.backgroundColor = [UIColor orangeColor];
//将后边的视图添加到前面的视图之上
[self.view addSubview:view];//
// //如果父视图不允许交互,那么字视图的事件也会被屏蔽
// self.view.userInteractionEnabled =NO;
//设置视图的标签
view.tag=1;
//设置透明度,0~1浮点
view.alpha=0.8;
// //如果父视图透明,那么子视图也会看不见。
// self.view.alpha=0;
//设置视图是否隐藏(默认NO)
view.hidden=NO;
// //父视图被隐藏,那么子视图也会被隐藏
// self.view.hidden=YES;
//获取父视图中标签为1的视图
UIView *view2=[self.view
viewWithTag:1];
for
(id
vi
in
self.view.subviews)
{ //打印视图的属性
NSLog(@"====%@",[vi class]);
NSLog(@"====%@",[vi class]);
}
UIView *view4=[[UIView
alloc]initWithFrame:CGRectMake(50,
40,
50,
50)];
view4.backgroundColor
=[UIColor
greenColor];
[self.view addSubview:view4];
//将view2添加到父视图且在view之下
[self.view insertSubview:view2 belowSubview:view];
//将view2添加到父视图,在view上
[self.view insertSubview:view2 aboveSubview:view];
//将子视图添加到父视图的某个位置
[self.view insertSubview:view2 atIndex:0];
//将视图移出父视图
[view removeFromSuperview];
//交换两个位置的视图
[self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
//将子视图移到父视图的最底层
[self.view sendSubviewToBack:view2];
//将子视图移到父视图的最前方
[self.view addSubview:view4];
//将view2添加到父视图且在view之下
[self.view insertSubview:view2 belowSubview:view];
//将view2添加到父视图,在view上
[self.view insertSubview:view2 aboveSubview:view];
//将子视图添加到父视图的某个位置
[self.view insertSubview:view2 atIndex:0];
//将视图移出父视图
[view removeFromSuperview];
//交换两个位置的视图
[self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
//将子视图移到父视图的最底层
[self.view sendSubviewToBack:view2];
//将子视图移到父视图的最前方
[self.view
bringSubviewToFront:view2];