Ui——创建视图的方法及过程
Posted 驭狼共舞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ui——创建视图的方法及过程相关的知识,希望对你有一定的参考价值。
//1.创建视图
ViewController.h
@interface ViewController : UIViewController
//创建视图控件;
@property(strong,nonatomic)UIView *myView;
@end
ViewController.m
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//结构体 确定矩形的区域,x,y,wedth,height;
CGRect rect=CGRectMake(10,10, 150, 500);//(x,y,w,h)
//实例化视图对象并指定视图区域;
UIView *myView=[[UIView alloc]initWithFrame:rect];
myView.backgroundColor=[UIColor redColor];//设置背景色;
UIView *myView=[[UIView alloc]initWithFrame:rect];
myView.backgroundColor=[UIColor redColor];//设置背景色;
[self.view addSubview:myView];//添加子视图带view上;
//查看视图View的CGRect。。。。。
/
//frame(框架) 相对于父视图的坐标及大小 bounds(界限) 只是显示当前视图的大小
//法1 。。。父视图view的frame;
CGRect rectView=self.view.frame;
NSLog(@"View.freme:%@",NSStringFromCGRect(rectView));
NSLog(@"View.freme:%@",NSStringFromCGRect(rectView));
//法2
//子视图myView的frame;
NSLog(@"myView.freme:%@",NSStringFromCGRect(self.myView.frame));
//子视图myView的bounds视图界限,坐标从(0,0)开始
NSLog(@"myView.bounds:%@",NSStringFromCGRect(self.myView.bounds));
//子视图myView的center中心点的坐标。。。。。。
NSLog(@"%@",NSStringFromCGPoint(self.myView.center));
//移动创建的子视图的位置(及改变中心点center的坐标Point);
self.myView.center=CGPointMake(100, 100);
//改变创建子视图的边界;
//移动创建的子视图的位置(及改变中心点center的坐标Point);
self.myView.center=CGPointMake(100, 100);
//改变创建子视图的边界;
self.myView.bounds=CGRectMake(0, 0, 150, 150);
}
// 2.视图的一些排版方法;
ViewController.h
@interface ViewController : UIViewController
@property(strong,nonatomic)UIView *aView;
@property(strong,nonatomic)UIView *bView;
@property(strong,nonatomic)UIView *cView;
@property(strong,nonatomic)UIView *aView;
@property(strong,nonatomic)UIView *bView;
@property(strong,nonatomic)UIView *cView;
@end
ViewController.m
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *aView=[[UIViewalloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
aView.backgroundColor=[UIColor redColor];
[self.view addSubview:aView];
UIView *bView=[[UIView alloc]initWithFrame:CGRectMake(150, 150, 150, 150)];
bView.backgroundColor=[UIColor greenColor];
[self.view addSubview:bView];
UIView *cView=[[UIView alloc]initWithFrame:CGRectMake(200, 200, 100, 100)];
cView.backgroundColor=[UIColor blueColor];
[self.view addSubview:cView];
//操作方法。。。。。。。。。。。。。
//插入指定视图放在---的下面
[self.view insertSubview:bView belowSubview:aView];
//插入指定视图放在---的上面
[self.view insertSubview:bView aboveSubview:aView];
//在指定位置插入视图
[self.view insertSubview:bView atIndex:0];
//移除视图
[self.view removeFromSuperview];
//交换子视图的索引位置;
[self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
//将子视图放到最后
[self.view sendSubviewToBack:cView];
////将子视图放到最前
[self.view addSubview:aView];
UIView *bView=[[UIView alloc]initWithFrame:CGRectMake(150, 150, 150, 150)];
bView.backgroundColor=[UIColor greenColor];
[self.view addSubview:bView];
UIView *cView=[[UIView alloc]initWithFrame:CGRectMake(200, 200, 100, 100)];
cView.backgroundColor=[UIColor blueColor];
[self.view addSubview:cView];
//操作方法。。。。。。。。。。。。。
//插入指定视图放在---的下面
[self.view insertSubview:bView belowSubview:aView];
//插入指定视图放在---的上面
[self.view insertSubview:bView aboveSubview:aView];
//在指定位置插入视图
[self.view insertSubview:bView atIndex:0];
//移除视图
[self.view removeFromSuperview];
//交换子视图的索引位置;
[self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
//将子视图放到最后
[self.view sendSubviewToBack:cView];
////将子视图放到最前
[self.view bringSubviewToFront:aView];
}
以上是关于Ui——创建视图的方法及过程的主要内容,如果未能解决你的问题,请参考以下文章