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看看:

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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值