0

在我的一个观点中,我正在为视图添加阴影。问题是阴影在左右边缘显示空白。我想删除这些空格。

这是我的代码:

UIView *myView = [[ISTView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 35)];
myView.backgroundColor = [UIColor greyColor];
[myView.layer setShadowOffset:CGSizeMake(0.0, 5.0)];
[myView.layer setShadowOpacity:0.8];
[myView.layer setShadowRadius:2.0];
[myView.layer setShadowColor:[UIColor blackColor].CGColor];
[self.view addSubview:myView];
[myView release];

这是我的观点的o / p:

在此处输入图像描述

4

4 回答 4

2

如果您想要没有副作用的均匀阴影,您可以在图形编辑器中绘制它,保存为 png 并将带有可拉伸图像的 UIImageView 放置在您的视图上。并且不要忘记将 clipToBounds 设置为 NO。

UIView *myView = [[ISTView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 35)];
myView.backgroundColor = [UIColor greyColor];
myView.clipToBounds = NO;

UIImageView *shadowView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 35, 320, 10)];
shadowView.image = [UIImage imageWithName:@"my-shadow.png"];
[myView addSubview:shadowView];
[shadowView release];

[self.view addSubview:myView];
[myView release];

系统在视图层次结构上方绘制缓存的现有图像比计算图层的阴影更便宜。

于 2011-05-18T08:55:15.997 回答
1

使用 shadowPath 使阴影大于视图

view.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, view.frame.size.width+30, view.frame.size.height)].CGPath;
于 2011-05-18T08:17:43.907 回答
0

试试这个,删除代码: [myView.layer setShadowRadius:2.0];

于 2014-02-24T07:21:27.653 回答
0

我能想到并且正在工作的一种解决方案是将视图的框架在 X 位置和宽度上调整 2 个像素:

UIView *myView = [[ISTView alloc] initWithFrame:CGRectMake(-2.0, 0.0, 324.0, 35)];

但这不是一种更清洁的方法。如果有人有更好的解决方案,请指导。

于 2011-05-18T08:11:19.033 回答