原文? http://blog.csdn.net/jymn_chen/article/details/38095585
之前一直都沒有搞懂clipsTobounds屬性的作用赴邻,前幾天又遇到了這個屬性嗜桌,這次終于弄明白了。
首先看看UIView的clipsToubounds屬性在SDK中的描述:
@property (nonatomic) BOOL clipsToBounds; // When YES, content and subviews are clipped to the bounds of the view. Default is NO.
這里的clip是修剪的意思,bounds是邊界的意思是滤馍,合起來就是:如果子視圖的范圍超出了父視圖的邊界横媚,那么超出的部分就會被裁剪掉考赛。
寫個Demo看看效果,代碼如下:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *greenView = [UIView new];
greenView.frame = CGRectMake(0, 0, 300, 300);
greenView.backgroundColor = [UIColor greenColor];
greenView.center = self.view.center;
greenView.clipsToBounds = YES;
[self.view addSubview:greenView];
UIView *redView = [UIView new];
redView.frame = CGRectMake(0, 0, 100, 400);
redView.backgroundColor = [UIColor redColor];
redView.center = self.view.center;
[greenView addSubview:redView];
}
運行結(jié)果如下:
將greenView的clipsTobounds屬性設(shè)為NO今豆,其它不做任何改動(注意redView還是greenView的子視圖)
greenView.clipsToBounds = NO;
再Run看看:
紅色視圖終于突破了綠色視圖的邊界嫌拣。
該屬性在實際工程中還是非常實用的,必須要了解清楚呆躲。