向视图层添加阴影时出现问题

发布于 2024-11-08 03:35:20 字数 550 浏览 0 评论 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:

在此处输入图像描述

In one of my view I am adding shadow to a view. Thing is that the shadow shows white spaces on left & right edges. I want to remove these spaces.

Here is my code:

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];

Here is my view's o/p:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黯然#的苍凉 2024-11-15 03:35:20

如果您想要均匀的阴影而没有副作用,您可以在图形编辑器中绘制它,保存为 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];

对于系统来说,在视图层次结构之上绘制缓存的现有图像比计算图层的阴影更便宜。

If you want homogenous shadow without side effects you can draw it in graphics editor, save to png and place UIImageView with stretchable image on your view. And don't forget to set clipToBounds to 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];

It would be cheaper for system to draw cached existing image above view hierarcy than calculate layer's shadow.

高跟鞋的旋律 2024-11-15 03:35:20

使用shadowPath使阴影大于视图

view.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, view.frame.size.width+30, view.frame.size.height)].CGPath;

Use shadowPath to make the shadow larger then the view

view.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, view.frame.size.width+30, view.frame.size.height)].CGPath;
流绪微梦 2024-11-15 03:35:20

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

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

但这并不是一种更干净的方法。如果谁有更好的解决方案,请指导。

One solution I could think of and is working also is to adjust the view's frame by 2 pixels in X position and width:

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

But this is not a cleaner approach of doing this. If anyone has better solution, please guide.

爱的故事 2024-11-15 03:35:20

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

Try this, remove the code:
[myView.layer setShadowRadius:2.0];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文