UIView clipsToBounds屬性

UIView 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];
}

運行結果如下:

將greenView的clipsTobounds屬性設爲NO,其它不做任何改動(注意redView還是greenView的子視圖)

greenView.clipsToBounds = NO;
再Run看看:

紅色視圖終於突破了綠色視圖的邊界。

該屬性在實際工程中還是非常實用的,必須要了解清楚。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章