xcode 在 UIView 中创建内阴影
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9214482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create inner shadow in UIView
提问by alexmngn
I would like create an inner shadow on my UIView
on iPad like that :
我想在我UIView
的 iPad 上像这样创建一个内部阴影:
This UIView
could change size so I can't use a simple image to create this kind of shadow.
这UIView
可能会改变大小,所以我不能使用简单的图像来创建这种阴影。
I have tested with setShadow
etc., but it's only a dropshadow that is created.
我已经测试过setShadow
等等,但它只是一个创建的阴影。
Any idea how to create this kind of shadow?
知道如何创建这种阴影吗?
采纳答案by Nick Lockwood
Create the shadow as a transparent layer at a particular size, then create a stretchable image, like this:
将阴影创建为特定大小的透明图层,然后创建一个可拉伸的图像,如下所示:
UIImage *shadowImage = [UIImage imageNamed:@"shadow.png"];
shadowImage = [shadowImage stretchableImageWithLeftCapWidth:floorf(shadowImage.size.width/2) topCapHeight:floorf(shadowImage.size.height/2)];
You can then put that as the image in a UIImageView with contentMode scale to fit and it will work the way you want.
然后,您可以将其作为图像放入 UIImageView 并使用 contentMode 缩放以适应,它会按照您想要的方式工作。
Lets say your view is called "myView". You can add the shadow like this:
假设您的视图称为“myView”。您可以像这样添加阴影:
UIImageView *shadowImageView = [[UIImageView alloc] initWithImage:shadowImage];
shadowImageView.contentMode = UIViewContentModeScaleToFill;
shadowImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
shadowImageView.frame = myView.bounds;
[myView addSubview:shadowImageView];
[shadowImageView release]; // only needed if you aren't using ARC
You can also do most of this in interface builder if you prefer, as long as you create the stretchable image itself in code.
如果您愿意,也可以在界面构建器中完成大部分操作,只要您在代码中创建可拉伸图像本身即可。
回答by Seitk
You may find it useful, I made an UIView Category for that
你可能会发现它很有用,我为此做了一个 UIView 类别
回答by Joel Kravets
This question was already partially answered in SO
这个问题已经在SO 中得到了部分回答
view.layer.masksToBounds = NO;
view.layer.cornerRadius = 8; // if you like rounded corners
view.layer.shadowRadius = 5;
view.layer.shadowOpacity = 0.5;
CGFloat indent = 10;
CGRect innerRect = CGRectMake(indent,indent,view.frame.size.width-2*indent,view.frame.size.height-2*indent);
view.layer.shadowPath = [UIBezierPath bezierPathWithRect:innerRect].CGPath;
Play around with the variables until they suit your shadow style.
使用变量直到它们适合您的阴影风格。
UPDATE
更新
Instead you can draw an outer shadow on a smaller centered view which lies on top of your existing view.
相反,您可以在位于现有视图顶部的较小居中视图上绘制外部阴影。
CGFloat indent = 10;
CGRect innerRect = CGRectMake(indent,indent,view.frame.size.width-2*indent,view.frame.size.height-2*indent);
UIView * shadowView = [[UIView alloc] initWithFrame:innerRect];
shadowView.backgroundColor = [UIColor clearColor];
shadowView.layer.masksToBounds = NO;
shadowView.layer.cornerRadius = 8; // if you like rounded corners
shadowView.layer.shadowRadius = 5;
shadowView.layer.shadowOpacity = 0.5;
shadowView.layer.shadowPath = [UIBezierPath bezierPathWithRect:shadowView.bounds].CGPath;
[view addSubview:shadowView];
[shadowView release];