UIWindow和UIView的简单使用

UIWindow是是一个窗体,用来显示组件,相当于世一个容器

1,创建UIWindow

//创建window 
self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
//设置window的背景
    self.window.backgroundColor=[UIColor whiteColor];
    //设置window显示的view
    ViewController *vc=[[ViewController alloc]init];
    //创建rootview
    UINavigationController *control=[[UINavigationController alloc]initWithRootViewController:vc];
    //将rootview设置成根view
    self.window.rootViewController=control;
    //显示window
    [self.window makeKeyAndVisible ];//设置window显示

UIScreed可以看成就是手机屏幕,它提供了一个画布,可以在上面画各种视图控件,可以通过[[UIScreen mainScreen] bounds]来获取屏幕尺寸;

makeKeyAndVisible,把window设置成可显 的。

2,获得window

 UIWindow *win=[UIApplicationsharedApplication].keyWindow;

 

 

 

3,获得父视图

    //获取父视图

    UIView *v=view1.superview;

 

    NSLog(@"%@",[v class]); //UIWindow

 

4,UIView常用的结构体

CGPoint point = CGPointMake(x,y);//左上角坐标的位置
CGSize size = CGSizeMake(width,height);//大小
CGRect rect = CGRectMake(x,y,width,height);//位置和大小

 

5,UI常用属性

frame: 相对父视图的位置和大小
bounds:相对自身的位置和大小,所以bounds的x和y永远为0
center:子视图的中点坐标相对父视图的位置
transform:可以通过这个属性控制视图的放大缩小和旋转
superview:获取父视图
subviews:获取所有子视图
alpha:视图的透明度(0-1)
tag:视图的标志,设置了tag后,可以通过viewWithTag方法拿到这个视图
userInteractionEnabled:是否响应用户事件

 

 

 

6,UIView的创建

  self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor=[UIColor whiteColor];
    
    ViewController *vc=[[ViewController alloc]init];
    
    UINavigationController *control=[[UINavigationController alloc]initWithRootViewController:vc];
    
    self.window.rootViewController=control;
    
    [self.window makeKeyAndVisible ];//设置window显示
    
    
    //UIView视图的管理
    UIView *view1=[[UIView alloc] init];
    view1.frame=CGRectMake(100, 100, 100, 100);
    view1.backgroundColor=[UIColor colorWithRed:0.5 green:0.3 blue:0.1 alpha:0.3];
     //将view1添加到window上显示
    [self.window addSubview:view1];
    
    UIView *view2=[[UIView alloc] init];
    view2.frame=CGRectMake(200, 100, 100, 100);
    view2.backgroundColor=[UIColor yellowColor];
    [self.window addSubview:view2];

 

在UIwindow上添加view1和view2(使用的是自动回收),

a,[self.window insertSubview:view2 atIndex:0];//添加到指定的位置

b,  //获取子视图

    NSArray *v1=self.window.subviews;

    NSLog(@"%@",v1);

c,// 视图把  移出,谁要移除来,谁就去调这个

    [ view2 removeFromSuperview];

d,view2.hidden=1;//隐藏View2

 

 7,UIView 常用用法

- (void)removeFromSuperview;将视图从父视图中移除
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;插入一个视图到指定位置,视图越在下面,index越小
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;将index1和index2位置的两个视图互换位置

- (void)addSubview:(UIView *)view;添加视图到父视图
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;插入视图到指定视图的下面
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;插入视图到指定视图上面

- (void)bringSubviewToFront:(UIView *)view;把视图移到最顶层
- (void)sendSubviewToBack:(UIView *)view;把视图移到最底层

- (UIView *)viewWithTag:(NSInteger)tag;     根据视图的tag属性找到搜索视图

猜你喜欢

转载自baihe747.iteye.com/blog/2265188