UIView详解1

一个UIView的实例就是一个视图,表示的是屏幕上的一块矩形区域,负责这块矩形区域的描绘以及和用户的交互。

第一、UIView的可视化属性

1. backgroundColor  背景属性

2. hidden  表示该view是否隐藏,

hidden属性为YES时视图隐藏,否则不隐藏。

3. alpha  为0时完全透明,为1时完全不透明

注意事项:

当视图完全透明或者隐藏时,不能响应触摸消息。

也就是alpha等于0.0或者hidden为YES的时候,但是当alpha<0.01的时候,视图就已经接收不到消息了。

 视图的alpha值会影响子视图的绘制,但是子视图的alpha值不变。

4. opaque  property
这是一个优化属性,如果该值为YES, 那么绘图在绘制该视图的时候把整个视图当作不透明对待。这样,绘图系统在执行绘图过程中会优化一些操作并提升系统性能;如果是设置为NO, 绘图系统将其和其他内容平等对待,不去做优化操作。为了性能方面的考量,默认被置为YES(意味着‘优化’)。
另一方面,这个消息和alpha 是有关系的。 一个不透明视图需要整个边界里面的内容都是不透明的。基于这个原因,opaque设置为YES,要求对应的alpha必须为1.0。如果一个UIView实例opaque被设置为YES, 而同时它又没有完全填充它的边界(bounds),或者它包含了整个或部分的透明的内容视图,那么将会导致未知的结果。
因此,如果视图部分或全部支持透明,那么你必须把opaque这个值设置为NO. 

5. clipsToBounds  

在类的层次结构中,如果clipsTobounds设为YES,超出superView的部分subview就不会显示,否则会做显示, 默认情况下是NO。

6. clearsContextBeforeDrawing  

A Boolean value that determines whether the receiver’s bounds should be automatically cleared before drawing.

7. layerClass和 layer  property 跟Core Animation layer有关

第二、管理视图层次

1. superview  property 返回该view的superView

sample:

    [self.windowaddSubview:self.viewController.view];

    NSLog(@"the superView is %@",[self.viewController.viewsuperview]);

2. subviews  property 返回该view的subviews

sample:

 

    NSLog(@"the suberViews are %@",[self.windowsubviews]);

3. window   property 返回该view的window,如果没有返回nil

    NSLog(@"the window is %@",[self.viewController.viewwindow]);

4. – addSubview:

5. – bringSubviewToFront:

把指定的子view放到最顶层,其父视图调用bringSubviewToFront()方法。

sample:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    UIButton *button=[[[UIButton alloc] initWithFrame:CGRectMake(120, 120, 50, 50)] autorelease];
    button.backgroundColor=[UIColor redColor];
    [self.view addSubview:button];
    
    UIButton *button2=[[[UIButton alloc] initWithFrame:CGRectMake(130, 130, 50, 50)] autorelease];
    button2.backgroundColor=[UIColor blueColor];

    [self.view addSubview:button2];
    [self.view bringSubviewToFront:button];
    
}

6.--sendSubviewToBack:

将一个UIView层推送到背后只需要调用其父视图的 sendSubviewToBack()方法。

sample:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    UIButton *button=[[[UIButton alloc] initWithFrame:CGRectMake(120, 120, 50, 50)] autorelease];
    button.backgroundColor=[UIColor redColor];
    [self.view addSubview:button];
    
    UIButton *button2=[[[UIButton alloc] initWithFrame:CGRectMake(130, 130, 50, 50)] autorelease];
    button2.backgroundColor=[UIColor blueColor];

    [self.view addSubview:button2];
    [self.view sendSubviewToBack:button2];
    
    //[self.view bringSubviewToFront:button];
    
}

7. removeFromSuperview

sample:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    UIButton *button=[[[UIButton alloc] initWithFrame:CGRectMake(120, 120, 50, 50)] autorelease];
    button.backgroundColor=[UIColor redColor];
    [self.view addSubview:button];
    
    UIButton *button2=[[[UIButton alloc] initWithFrame:CGRectMake(130, 130, 50, 50)] autorelease];
    button2.backgroundColor=[UIColor blueColor];

    [self.view addSubview:button2];
  
    [button removeFromSuperview];
}

8. – insertSubview:atIndex:

Parameters

view

The view to insert. This value cannot be nil.

index

The index in the array of the subviews property at which to insert the view. Subview indices start at0 and cannot be greater than the number of subviews.

sample:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    UIButton *button=[[[UIButton alloc] initWithFrame:CGRectMake(120, 120, 50, 50)] autorelease];
    button.backgroundColor=[UIColor redColor];
    [self.view addSubview:button];
    
    UIButton *button2=[[[UIButton alloc] initWithFrame:CGRectMake(130, 130, 50, 50)] autorelease];
    button2.backgroundColor=[UIColor blueColor];

    [self.view addSubview:button2];
    
    
    UIButton *button3=[[[UIButton alloc] initWithFrame:CGRectMake(120, 120, 100, 100)] autorelease];
    button3.backgroundColor=[UIColor greenColor];
    [self.view insertSubview:button3 atIndex:2];
}

9.– insertSubview:aboveSubview:

Inserts a view above another view in the view hierarchy.

- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview

sample:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    UIButton *button=[[[UIButton alloc] initWithFrame:CGRectMake(120, 120, 50, 50)] autorelease];
    button.backgroundColor=[UIColor redColor];
    [self.view addSubview:button];
    
    UIButton *button2=[[[UIButton alloc] initWithFrame:CGRectMake(130, 130, 50, 50)] autorelease];
    button2.backgroundColor=[UIColor blueColor];

    [self.view addSubview:button2];
    
    
    UIButton *button3=[[[UIButton alloc] initWithFrame:CGRectMake(120, 120, 100, 100)] autorelease];
    button3.backgroundColor=[UIColor greenColor];

    [self.view insertSubview:button3 aboveSubview:button];
}

10.– insertSubview:belowSubview:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    UIButton *button=[[[UIButton alloc] initWithFrame:CGRectMake(120, 120, 50, 50)] autorelease];
    button.backgroundColor=[UIColor redColor];
    [self.view addSubview:button];
    
    UIButton *button2=[[[UIButton alloc] initWithFrame:CGRectMake(130, 130, 50, 50)] autorelease];
    button2.backgroundColor=[UIColor blueColor];

    [self.view addSubview:button2];
    
    
    UIButton *button3=[[[UIButton alloc] initWithFrame:CGRectMake(120, 120, 100, 100)] autorelease];
    button3.backgroundColor=[UIColor greenColor];
    [self.view insertSubview:button3 belowSubview:button];
}

11.– exchangeSubviewAtIndex:withSubviewAtIndex:

- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2

Parameters

index1

The index of the first subview in the receiver.

index2

The index of the second subview in the receiver.

sample:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    UIButton *button=[[[UIButton alloc] initWithFrame:CGRectMake(120, 120, 50, 50)] autorelease];
    button.backgroundColor=[UIColor redColor];
    [self.view addSubview:button];
    
    UIButton *button2=[[[UIButton alloc] initWithFrame:CGRectMake(130, 130, 50, 50)] autorelease];
    button2.backgroundColor=[UIColor blueColor];

    [self.view addSubview:button2];
    
    
    UIButton *button3=[[[UIButton alloc] initWithFrame:CGRectMake(120, 120, 100, 100)] autorelease];
    button3.backgroundColor=[UIColor greenColor];
    [self.view addSubview:button3];
    [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:3];
}


12 – isDescendantOfView:

Returns a Boolean value indicating whether the receiver is a subview of a given view or identical to that view.

- (BOOL)isDescendantOfView:(UIView *)view

sample:

    NSLog(@"the result is %d",[button3isDescendantOfView:self.view]);





原文地址:https://www.cnblogs.com/james1207/p/3297221.html