![](/img/trans.png)
[英]How to add shadow to only top side of UIView without using shadow path and adding layers
[英]How to get Shadow on UIView as from Top side Only ? [ Objective-c ]
我只希望如下图所示从顶部的UIView
阴影。
我正在尝试此代码来应用shadow,但是shadow应用于视图的右侧,但我希望从顶部开始。
UIEdgeInsets contentInsets = UIEdgeInsetsMake(10, 0, 0, 0);
CGRect shadowPath = UIEdgeInsetsInsetRect(self.dealMainContentView.bounds, contentInsets);
self.dealMainContentView.layer.shadowPath = [UIBezierPath bezierPathWithRect:shadowPath].CGPath;
self.dealMainContentView.layer.shadowColor = [[UIColor blackColor] CGColor];
self.dealMainContentView.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
self.dealMainContentView.layer.shadowOpacity = 0.4f;
self.dealMainContentView.layer.masksToBounds=NO;
这看起来应该像
我自己解决了这个问题,可以在制作Path之前通过修改帧来获得所需的阴影。
只是hack :)
我没有在这种方法中传递view.frame,而是修改了框架
UIEdgeInsetsInsetRect(viewFrame, contentInsets);
并从顶部产生阴影。
这是下面的代码
UIEdgeInsets contentInsets = UIEdgeInsetsMake(-10, -10, 0, 0);
CGRect viewFrame=self.dealMainContentView.frame;
viewFrame.origin.x += 5; // 5 distance from left
viewFrame.origin.y -= 15;
viewFrame.size.width -= 1o; // 5 distance from Right
viewFrame.size.height -= 20;
CGRect shadowPathExcludingTop = UIEdgeInsetsInsetRect(viewFrame, contentInsets);
self.dealMainContentView.layer.shadowPath = [UIBezierPath bezierPathWithRect:shadowPathExcludingTop].CGPath;
self.dealMainContentView.layer.shadowColor = [[UIColor blackColor] CGColor];
self.dealMainContentView.layer.shadowOffset = CGSizeMake(0.0,0.0f);
self.dealMainContentView.layer.shadowOpacity = 0.3f;
self.dealMainContentView.layer.masksToBounds=NO;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.