我使用圆角corners
将阴影添加到视图中并且它可以正常工作。但是,当frame
视图更改shadow
时,根据视图不会更改其大小。
我尝试过以下方法:
-(void)addShadow:(UIView *)view withCornerRad : (int)radius{
view.clipsToBounds = YES;
CALayer *ViewLayer = view.layer;
[ViewLayer setMasksToBounds:NO ];
ViewLayer.shadowColor = [UIColor lightGrayColor].CGColor;
ViewLayer.shadowOpacity = 1.0 ;
ViewLayer.shadowRadius = 2.0 ;
ViewLayer.shadowOffset = CGSizeMake( 0 , 0 );
ViewLayer.cornerRadius = radius;
ViewLayer.shadowPath = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
}
答案 0 :(得分:6)
根据Apples view programming guide:
视图负责绘制内容,处理多点触控事件以及管理任何子视图的布局。
所以你可以用两种方式改变你的影子: 1.通过使用覆盖layoutSubviews函数对UIView进行子类化,您可以在其中设置新的阴影大小。 2.您可以覆盖控制器viewDidLayoutSubviews()方法,您可以在其中设置新的阴影大小。
祝你好运!
答案 1 :(得分:0)
-(void)addShadow:(UIView *)view withCornerRad : (int)radius
{
view.clipsToBounds = YES;
CALayer *ViewLayer = view.layer;
ViewLayer.shadowColor = [UIColor lightGrayColor].CGColor;
ViewLayer.shadowRadius = 10;
ViewLayer.shadowOffset = CGSizeMake(0.0f, 0.0f);
ViewLayer.shadowOpacity = 2;
ViewLayer.masksToBounds = NO;
UIEdgeInsets shadowInsets = UIEdgeInsetsMake(0, 0, 0, 0);
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:UIEdgeInsetsInsetRect(view.bounds, shadowInsets)];
ViewLayer.shadowPath = shadowPath.CGPath;
}
如果您想要阴影的顶部,左侧,底部,右侧视图
,请尝试此操作