[英]How to adjust shadow dynamically with height of view in ios?
I added the shadow to the view with rounded corners
and it works. 我加了阴影带圆角的观点corners
和它的作品。 But when frame
of view changes shadow
won't change its size according to view. 但是,当frame
的视图改变shadow
根据图将不改变其大小。 I have tried the following: 我尝试了以下方法:
-(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;
}
According to Apples view programming guide : 根据苹果公司的视图编程指南 :
Views are responsible for drawing content, handling multitouch events, and managing the layout of any subviews. 视图负责绘制内容,处理多点触控事件以及管理任何子视图的布局。
So you can change your shadow in two ways: 1. By subclassing UIView with overriding layoutSubviews function, where you can set new shadow size. 因此,您可以通过两种方式更改阴影:1.通过使用覆盖的layoutSubviews函数对UIView进行子类化,可以在其中设置新的阴影大小。 2. You can override your controllers viewDidLayoutSubviews() method, where you can set new shadow size. 2.您可以覆盖控制器的viewDidLayoutSubviews()方法,在其中可以设置新的阴影大小。
Best regards! 最好的祝福!
-(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;
}
try this if you want shaddow top,left ,bottom, right side of view 如果要从阴影顶部,左侧,底部,右侧尝试此操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.