UI基础学习之(一):UIWindow、UIView的基本属性

来源:互联网 发布:光翼学园网络班学费 编辑:程序博客网 时间:2024/05/18 03:09

UIWindow

1、每个程序至少有一个Window,在Application.m中进行设置

2、Window用来展示控件

-(BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds] autorelease]; //注意:释放使用autoreleases

    self.window.backgroundColor = [UIColorblueColor];

    [self.window makeKeyAndVisible];

     return YES;

}

说明:

[UIScreen mainScreen].bounds  获取屏幕大小,将Window的Frame设置为和屏幕的大小一致。


UIView:

UIView是所有控件的父类,一般作为容器使用,UIView的属性其他控件都进行了继承。

创建UIView:

UIView * view = [[UIView alloc]initWithFrame:CGRectMake(150, 100, 100, 100)];

//    设置颜色(默认透明色)

    view.backgroundColor = [UIColor redColor];

//    将View添加到Window上

    [self.window addSubview:view];   

//    Frame

//    设置X,Y,W,H 决定View显示的位置和大小

    view.frame = CGRectMake(200, 100, 100,100);

//    释放View

[view release];


视图的重要属性

    UIView * view = [[UIView alloc] init];

    view.frame = CGRectMake(100, 100, 100,100);

    view.backgroundColor = [UIColor blueColor];

//    设置中心点 改变中心点可以改变视图位置

    view.center = CGPointMake(300, 300);  

//    是否隐藏 默认值:no

    view.hidden = NO;

//    透明度 取值范围 0~1

    view.alpha = 0.5;

//    tag值,便于确定视图, 从100开始

    view.tag = 100;

    [self.window addSubview:view];

//    获取父视图

    NSLog(@"%@", view.superview);

//    获取子视图 数组

    NSLog(@"%@", self.window.subviews);

//    通过tag值来获取视图

    UIView * tmp = [self.windowviewWithTag:100];

    NSLog(@"%@", tmp);



添加视图的方法(4种)

  UIView * redView =[[UIView alloc] init];

  redView.frame =CGRectMake(100, 100, 100, 100);

  redView.backgroundColor= [UIColor redColor];

//添加到window

  [self.windowaddSubview:redView];

  

UIView * blueView =[[UIView alloc] init];

blueView.frame =CGRectMake(100, 100, 100, 100);

blueView.backgroundColor= [UIColor blueColor];

[self.windowaddSubview:blueView];

  

UIView * yellowView= [[UIView alloc] init];

yellowView.frame =CGRectMake(100, 100, 100, 100);

yellowView.backgroundColor= [UIColor yellowColor];

//    添加到指定位置

 [self.window insertSubview:yellowViewatIndex:1];

//    将视图添加到指定视图上面

 [self.window insertSubview:yellowViewaboveSubview:blueView]; 

//    将视图添加到指定视图下面

 [self.window insertSubview:yellowViewbelowSubview:redView];


视图层次管理

//    调整视图的位置 4种

//    将指定的视图放到最前面

[self.window  bringSubviewToFront: redView];   

//    将指定视图放到最后面

 [self.window   sendSubviewToBack:blueView];

//    交换两个指定视图

 [self.window   exchangeSubviewAtIndex:0withSubviewAtIndex:2];

//    移除视图

 [blueView   removeFromSuperview];


 

Bounds

    UIView *redView = [[UIView alloc] init];

    redView.frame = CGRectMake(100, 100, 100,100);

    redView.backgroundColor = [UIColorredColor];

    [self.window addSubview:redView];

   

//    frame:视图在父视图坐标系的位置和大小

    NSLog(@"redView frame : %@",NSStringFromCGRect(redView.frame));

   

//    bounds:视图在本身视图坐标系下的位置和大小

    NSLog(@"redView.bounds: %@",NSStringFromCGRect(redView.bounds));

   

//    blueView作为redView的子视图

    UIView *blueView = [[UIView alloc] init];

    blueView.frame = CGRectMake(0, 0, 50, 50);

    blueView.backgroundColor = [UIColorblueColor];

    [redView addSubview:blueView];  

//    对redview的bounds做修改

//    只修改宽高

    redView.bounds = CGRectMake(0, 0, 200,200);

//    只修改x y

//    一般不要修改bounds

    redView.bounds = CGRectMake(50, 50, 200,200); 

 

关于父视图和子视图

子视图依据父视图坐标决定自己的位置,子视图的坐标系依据父视图的坐标系设定。

    UIView * view1 = [[UIView alloc] init];

    view1.frame = CGRectMake(50, 50, 200, 200);

    view1.backgroundColor = [UIColor redColor];

//    view1是Window的子视图,Window是view1的父视图

    [self.window addSubview:view1];


    UIView * view2 = [[UIView alloc] init];

    view2.frame = CGRectMake(50, 50, 100, 100);

    view2.backgroundColor = [UIColorblueColor]; 

//    view1是view2的父视图,view2是view1的子视图

    [view1 addSubview:view2];


 
0 0
原创粉丝点击