视图的层次结构(一)

2015-01-27 14:07:53 · 作者: · 浏览: 45

AppDelegate.m

#import "AppDelegate.h"
#import "TestView.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //设置未读信息数
//    application.applicationIconBadgeNumber = 9;
    
    /*______________________________frame、bounds和center_____________________________________*/
    //坐标系的零点是在状态栏上面
    //创建视图
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 20, 90, 90)];
    //设置视图的颜色
    view.backgroundColor = [UIColor redColor];
    
    //以其父视图的起点为原点
    view.frame = CGRectMake(0, 20, 320, 40);    //center: 160  40
    
    //错误,  bounds的x和y必须为零,它可以改变设置大小,不可以改变位置
//    view.bounds = CGRectMake(10, 20, 90, 90);
    
    //center是用来表示视图的中心位置的,可以改变视图的位置,不可以改变视图的大小
//    view.center = CGPointMake(160, 120);
    
    CGRect bounds = view.bounds;
    //NSStringFromCGRect将结构体类型的CGRect转换成字符串
    NSString *str = NSStringFromCGRect(bounds);
    NSLog(@"str:%@",str);
    
    //添加到window上显示
    [self.window addSubview:view];
    [view release];

    /*__________________________视图的层次结构___________________________*/
    
    //创建父视图
    UIView *superView = [[UIView alloc] initWithFrame:CGRectMake(0, 80, 320, 300)];
    superView.backgroundColor = [UIColor blackColor];
    //添加到window上面显示
    [self.window addSubview:superView];//retain
    [superView release];
    
    //给superView添加子视图
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 300, 200)];
    view1.backgroundColor = [UIColor greenColor];
    //添加到supView上面显示
    [superView addSubview:view1];
    [view1 release];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(20, 40, 300, 160)];
    view2.backgroundColor = [UIColor orangeColor];
    //添加到supView上显示
//    [superView addSubview:view2];
    
    //将视图插入到superView子视图的下面,索引为0
    [superView insertSubview:view2 atIndex:0];
    
    //将view2作为supView的子视图插入到view1的上面
    [superView insertSubview:view2 aboveSubview:view1];
    
    //将view2作为supView的子视图插入到view1的下面
    [superView insertSubview:view2 belowSubview:view1];
    
    [view2 release];
    
    //将view2作为supView的子视图移动到最上面
    [superView bringSubviewToFront:view2];
    
    //将view2作为supView的子视图移动到最下面
    [superView sendSubviewToBack:view2];
    
    
//交换superView两个子视图的位置
[superView exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; //取得当前视图的父视图view1.superview; NSLog(@"%@",view1.superview); //如果想判断一个视图是否在显示,可以判断view.superview是否存在 //将view1从父视图上移除 if (view1.superview != nil) { [view1 removeFromSuperview]; } //注意: // removeFromSuperview只是将视图从父视图上面移除,但是不是销毁,还是在内存中存在的 NSLog(@"view1:%@",view1); /*__________________________UIView的常用属性___________________________*/ //1.透明度,范围是 [0,1] //注意:如果你设置父视图的透明度,子视图的这个属性也会跟着变化 // superView.alpha = .5; // view2.alpha = .5; //2.背景颜色 // clearColor是透明的 superView.backgroundColor = [UIColor greenColor]; //用过三色值设置颜色 // superView.backgroundColor = [UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>] //3.subviews得到视图的所有子视图 NSArray *views = superView.subviews; for (UIView *view in views) { view.backgroundColor = [UIColor darkGrayColor]; } //4.hidden 隐藏 // view2.hidden = YES; //5.superview得到当前视图的父视图 NSLog(@"supView:%@",superView); NSLog(