clipsTobounds属性的作用

转:http://blog.csdn.net/jymn_chen/article/details/38095585

之前一直都没有搞懂clipsTobounds属性的作用,前几天又遇到了这个属性,这次终于弄明白了。

首先看看UIView的clipsToubounds属性在SDK中的描述:

 

  1. @property (nonatomic) BOOL clipsToBounds; // When YES, content and subviews are clipped to the bounds of the view. Default is NO.  


这里的clip是修剪的意思,bounds是边界的意思是,合起来就是:如果子视图的范围超出了父视图的边界,那么超出的部分就会被裁剪掉。

 

写个Demo看看效果,代码如下:

 

  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.       
  4.     UIView *greenView = [UIView new];  
  5.     greenView.frame = CGRectMake(0, 0, 300, 300);  
  6.     greenView.backgroundColor = [UIColor greenColor];  
  7.     greenView.center = self.view.center;  
  8.     greenView.clipsToBounds = YES;  
  9.     [self.view addSubview:greenView];  
  10.       
  11.     UIView *redView = [UIView new];  
  12.     redView.frame = CGRectMake(0, 0, 100, 400);  
  13.     redView.backgroundColor = [UIColor redColor];  
  14.     redView.center = self.view.center;  
  15.     [greenView addSubview:redView];  
  16. }  


运行结果如下:

 

 

将greenView的clipsTobounds属性设为NO,其它不做任何改动(注意redView还是greenView的子视图)

 

  1. greenView.clipsToBounds = NO;  


再Run看看:

 

红色视图终于突破了绿色视图的边界。

该属性在实际工程中还是非常实用的,必须要了解清楚。

posted @ 2015-08-20 15:13  梦蝶Two  阅读(492)  评论(0编辑  收藏  举报