带有圆角和阴影的 UIView? [英] UIView with rounded corners and drop shadow?
问题描述
我已经在一个应用程序上工作了几年,并收到了一个简单的设计请求:在 UIView 上转角并添加一个投影.按照下面给出的操作.
I’ve been working on an application for a couple of years and received a simple design request: Round the corners on a UIView and add a drop shadow.To do as given below.
我想要一个自定义的 UIView
... :我只想要一个带圆角的空白视图和一个浅色阴影(没有灯光效果).我可以一一执行,但通常会发生 clipToBounds
/maskToBounds
冲突.
I want a custom UIView
... : I just wanted a blank white view with rounded corners and a light drop shadow (with no lighting effect). I can do each of those one by one but the usual clipToBounds
/maskToBounds
conflicts occur.
推荐答案
以下代码片段为 v
添加了边框、边框半径和阴影,一个 UIView
:
The following code snippet adds a border, border radius, and drop shadow to v
, a UIView
:
// border radius
[v.layer setCornerRadius:30.0f];
// border
[v.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[v.layer setBorderWidth:1.5f];
// drop shadow
[v.layer setShadowColor:[UIColor blackColor].CGColor];
[v.layer setShadowOpacity:0.8];
[v.layer setShadowRadius:3.0];
[v.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
Swift 5 版本:
Swift 5 Version :
// border radius
v.layer.cornerRadius = 30.0
// border
v.layer.borderColor = UIColor.lightGray.cgColor
v.layer.borderWidth = 1.5
// drop shadow
v.layer.shadowColor = UIColor.black.cgColor
v.layer.shadowOpacity = 0.8
v.layer.shadowRadius = 3.0
v.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
您可以调整设置以满足您的需要.
You can adjust the settings to suit your needs.
此外,将 QuartzCore 框架添加到您的项目中,并且:
Also, add the QuartzCore framework to your project and:
#import <QuartzCore/QuartzCore.h>
请参阅我的其他答案关于masksToBounds
.
注意
这可能不适用于所有情况.如果您发现此方法干扰了您正在执行的其他绘图操作,请参阅此答案.
This may not work in all cases. If you find that this method interferes with other drawing operations that you are performing, please see this answer.
这篇关于带有圆角和阴影的 UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!