UIView有圆角和投影?

Adi*_*yam 367 iphone cocoa-touch rounded-corners uiview ios

我真的尝试了所有我能做到的但是没有一个能用于定制UIView......我只是想要一个带圆角的白色空白视图和一个浅色阴影(没有光照效果).我可以一个接一个地做这些,但通常clipToBounds/ maskToBounds冲突发生.

Sur*_*gch 583

迅速

在此输入图像描述

// corner radius
blueView.layer.cornerRadius = 10

// border
blueView.layer.borderWidth = 1.0
blueView.layer.borderColor = UIColor.black.cgColor

// shadow
blueView.layer.shadowColor = UIColor.black.cgColor
blueView.layer.shadowOffset = CGSize(width: 3, height: 3)
blueView.layer.shadowOpacity = 0.7
blueView.layer.shadowRadius = 4.0
Run Code Online (Sandbox Code Playgroud)

探索选项

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

问题1:阴影被剪掉了

如果有子图层或子视图(如图像)我们想要剪切到视图边界的内容怎么办?

在此输入图像描述

我们可以做到这一点

blueView.layer.masksToBounds = true
Run Code Online (Sandbox Code Playgroud)

(或者,blueView.clipsToBounds = true给出相同的结果.)

在此输入图像描述

但是,哦不!阴影也被剪掉了,因为它超出界限!该怎么办?该怎么办?

为阴影和边框使用单独的视图.基本视图是透明的并且具有阴影.边框视图会剪切其边框所具有的任何其他子内容.

// add the shadow to the base view
baseView.backgroundColor = UIColor.clear
baseView.layer.shadowColor = UIColor.black.cgColor
baseView.layer.shadowOffset = CGSize(width: 3, height: 3)
baseView.layer.shadowOpacity = 0.7
baseView.layer.shadowRadius = 4.0

// add the border to subview
let borderView = UIView()
borderView.frame = baseView.bounds
borderView.layer.cornerRadius = 10
borderView.layer.borderColor = UIColor.black.cgColor
borderView.layer.borderWidth = 1.0
borderView.layer.masksToBounds = true
baseView.addSubview(borderView)

// add any other subcontent that you want clipped
let otherSubContent = UIImageView()
otherSubContent.image = UIImage(named: "lion")
otherSubContent.frame = borderView.bounds
borderView.addSubview(otherSubContent)
Run Code Online (Sandbox Code Playgroud)

这给出了以下结果:

在此输入图像描述

问题2:表现不佳

添加圆角和阴影可能会影响性能.您可以通过使用阴影的预定义路径并指定它进行栅格化来提高性能.以下代码可以添加到上面的示例中.

baseView.layer.shadowPath = UIBezierPath(roundedRect: baseView.bounds, cornerRadius: 10).cgPath
baseView.layer.shouldRasterize = true
baseView.layer.rasterizationScale = UIScreen.main.scale
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅此帖子.看到这里这里.

这个答案是用Swift 4和Xcode 9测试的.

  • 这对我不起作用。当我在 baseView 上将背景颜色设置为 clear 时,不再出现阴影。我究竟做错了什么? (3认同)
  • 不工作,设置`baseView.backgroundColor = UIColor.clear`删除阴影.只有设置背景颜色才会看到它. (3认同)
  • 不适合我。 (3认同)
  • 仅供参考,我最初看到的是与其他评论者看到的相同的问题,即当背景色清晰时,baseView的阴影未显示。问题是我只运行了代码的第一部分(baseView内容)。一旦将borderView作为子视图添加,阴影就会开始显示。似乎要显示阴影,它的视图层次结构中必须至少有一个可见边框(或背景)。因此,请确保borderView.layer.borderWidth> = 0且具有非透明的borderView.layer.borderColor(或非透明的背景色) (3认同)

Eva*_*ski 433

以下代码段添加了边框,边框半径和投影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)];
Run Code Online (Sandbox Code Playgroud)

您可以根据需要调整设置.

另外,将QuartzCore框架添加到您的项目中:

#import <QuartzCore/QuartzCore.h>
Run Code Online (Sandbox Code Playgroud)

我的回答其他有关masksToBounds.


注意

这可能不适用于所有情况.如果您发现此方法会干扰您正在执行的其他绘图操作,请参阅此答案.

  • 那么问题是当我设置角半径时,它设置maskToBounds:YES,而阴影需要clipToBounds:NO(其中clipToBounds与maskToBounds相同) (80认同)
  • 让它正常工作的方法*正确的方法是使用内部容器视图,它将容纳边框和背景颜色,两者都有一个圆角半径.这个视图将被限制为边界!第二个外部容器视图将容纳第一个,具有相同的框架,只有一个投影.我已经做了很多次来组合边框,阴影和角半径.这真的很烦人,但效果确实很好. (34认同)
  • 不行.不知道为什么会有这么多的选票.这适用于旧版本吗? (20认同)
  • 同样的问题在这里 如果我有背景颜色,我希望将其剪裁为圆角.要做到这一点,我必须使用maskToBounds = TRUE,但然后阴影消失了.. (13认同)
  • 对于像我这样的新手:我必须将QuartzCore框架导入到我的项目中,以便在图层对象上调用方法. (3认同)
  • 关注性能的人:这不是一个快速的操作.例如,如果要在表视图的每一行中向按钮添加投影,则可能会导致一些缓慢的过渡和滚动.将阴影预渲染为图像并使用它将更有效. (2认同)
  • 不使用maskToBounds/clipToBounds = YES (2认同)
  • 这不起作用.执行OP要求的唯一方法是使用两个视图,如@ Kpmurphy91所述. (2认同)

小智 77

一种方法是在带阴影的视图中放置带圆角的视图.

UIView* roundedView = [[UIView alloc] initWithFrame: frame];
roundedView.layer.cornerRadius = 5.0;
roundedView.layer.masksToBounds = YES;

UIView* shadowView = [[UIView alloc] initWithFrame: frame];
shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
shadowView.layer.shadowRadius = 5.0;
shadowView.layer.shadowOffset = CGSizeMake(3.0, 3.0);
shadowView.layer.shadowOpacity = 1.0;
[shadowView addSubview: roundedView];
Run Code Online (Sandbox Code Playgroud)

然后,您可以在任何地方添加shadowView.

  • Amit,你必须为*roundedView only*设置maskToBounds/clipToBounds = YES.不要将其设置为shadowView.我没有尝试过上面的代码,但确定这个解决方案确实有效但不理想.较高的shadowRadius负责角半径区域.将shadowRadius设置为0或1,您将注意到我想说的内容. (5认同)
  • "shadowView.layer.opacity = 1.0"应为"shadowView.layer.shadowOpacity = 1.0" (3认同)
  • 像shadowView.layer.shadowOpacity = 0.6; 不见了 (2认同)

Ser*_*yov 46

简单的Swift 4解决方案基于Ade的答案,没有任何额外的子视图,子类化或额外的摆弄:

extension UIView {

    func addShadow(offset: CGSize, color: UIColor, radius: CGFloat, opacity: Float) {
        layer.masksToBounds = false
        layer.shadowOffset = offset
        layer.shadowColor = color.cgColor
        layer.shadowRadius = radius
        layer.shadowOpacity = opacity

        let backgroundCGColor = backgroundColor?.cgColor
        backgroundColor = nil
        layer.backgroundColor =  backgroundCGColor
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,调用之前,应该使用角半径和其他属性设置视图addShadow.

直接来自viewDidLoad这样的调用:

btnBottom.addShadow(offset: CGSize.init(width: 0, height: 3), color: UIColor.black, radius: 2.0, opacity: 0.35)
Run Code Online (Sandbox Code Playgroud)

最后结果:

结果

超级简单!

编辑: 我看到很多人对这个例子有一些问题,所以我创建了一个简单的Xcode项目来说明它应该如何使用.

  • 它不适用于我们应用角半径. (15认同)
  • 这也对我有用,只需要一件事就是清除所有子视图的背景颜色,以便只有容器视图具有可见的背景,这解决了我的问题。谢谢!!@SergeyGrischyov (3认同)
  • 即使使用cornerRadius也可以正常工作. (2认同)

小智 40

这对我有用.特技是将背景颜色从主视图移动到图层.

CALayer *layer = view.layer;
layer.cornerRadius = 15.0f;
layer.masksToBounds = NO;

layer.shadowOffset = CGSizeMake(0, 3);
layer.shadowColor = [[UIColor blackColor] CGColor];
layer.shadowRadius = 2.0f;
layer.shadowOpacity = 0.35f;
layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:layer.bounds cornerRadius:layer.cornerRadius] CGPath];

CGColorRef  bColor = view.backgroundColor.CGColor;
view.backgroundColor = nil;
layer.backgroundColor =  bColor ;
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的解决方案 (3认同)

Ale*_*one 26

在为容器视图指定阴影路径时,我使用以下技巧解决了问题:

[UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:12]
Run Code Online (Sandbox Code Playgroud)

请注意,给阴影的路径是一个圆角矩形,其角半径与单元格包含的背景相同:

//this is the border for the UIView that is added to a cell
cell.backgroundView.layer.cornerRadius = 12;
cell.backgroundView.layer.masksToBounds = YES;
cell.backgroundView.layer.borderColor = [UIColor darkGrayColor].CGColor;
cell.backgroundView.layer.borderWidth = 1;

//this is the shadow around the cell itself (cannot have round corners with borders and shadow, need to use two views
cell.layer.shadowRadius = 2;
cell.layer.cornerRadius = 12;
cell.layer.masksToBounds = NO;
[[cell layer] setShadowColor:[[UIColor darkGrayColor] CGColor]];

[[cell layer] setShadowOffset:CGSizeMake(0.0,0.0)];
[[cell layer] setShadowOpacity:1.0];

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:12];
[[cell layer] setShadowPath:[path CGPath]];
Run Code Online (Sandbox Code Playgroud)


dan*_*ndi 17

如果你因为圆形cornersvs. subviewsvs. 而苦苦挣扎masksToBounds,那么尝试使用我的函数:

- (UIView*)putView:(UIView*)view insideShadowWithColor:(UIColor*)color andRadius:(CGFloat)shadowRadius andOffset:(CGSize)shadowOffset andOpacity:(CGFloat)shadowOpacity
{
    CGRect shadowFrame; // Modify this if needed
    shadowFrame.size.width = 0.f;
    shadowFrame.size.height = 0.f;
    shadowFrame.origin.x = 0.f;
    shadowFrame.origin.y = 0.f;
    UIView * shadow = [[UIView alloc] initWithFrame:shadowFrame];
    shadow.userInteractionEnabled = NO; // Modify this if needed
    shadow.layer.shadowColor = color.CGColor;
    shadow.layer.shadowOffset = shadowOffset;
    shadow.layer.shadowRadius = shadowRadius;
    shadow.layer.masksToBounds = NO;
    shadow.clipsToBounds = NO;
    shadow.layer.shadowOpacity = shadowOpacity;
    [view.superview insertSubview:shadow belowSubview:view];
    [shadow addSubview:view];
    return shadow;
}
Run Code Online (Sandbox Code Playgroud)

在你的观点上称呼它.你的视角是否有圆角,无论它的大小,形状 - 都会画出漂亮的阴影.

只需保留函数的返回值,以便在想要删除表时(或者例如使用insertSubview:aboveView:)可以引用它

  • 此解决方案适用于带圆角的UITableViews.希望我能更多地投票.谢谢! (2认同)

rba*_*win 11

使用Swift 4和Xcode 9,这是一个ImageView使用投影和边框进行舍入的工作示例.

    //set dimensions and position of image (in this case, centered)
    let imageHeight: CGFloat = 150, imageWidth: CGFloat = 150
    let xPosition = (self.view.frame.width / 2) - (imageWidth / 2)
    let yPosition = (self.view.frame.height / 2) - (imageHeight / 2)

    //set desired corner radius
    let cornerRadius: CGFloat = 20

    //create container for the image
    let imageContainer = UIView(frame: CGRect(x: xPosition, y: yPosition, width: imageWidth, height: imageHeight))

    //configure the container
    imageContainer.clipsToBounds = false
    imageContainer.layer.shadowColor = UIColor.black.cgColor
    imageContainer.layer.shadowOpacity = 1
    imageContainer.layer.shadowOffset = CGSize(width: 3.0, height: 3.0)
    imageContainer.layer.shadowRadius = 5
    imageContainer.layer.shadowPath = UIBezierPath(roundedRect: imageContainer.bounds, cornerRadius: cornerRadius).cgPath

    //create imageView
    let imageView = UIImageView(frame: imageContainer.bounds)

    //configure the imageView
    imageView.clipsToBounds = true
    imageView.layer.cornerRadius = cornerRadius
    //add a border (if required)
    imageView.layer.borderColor = UIColor.black.cgColor
    imageView.layer.borderWidth = 1.0
    //set the image
    imageView.image = UIImage(named: "bird")

    //add the views to the superview
    view.addSubview(imageContainer)
    imageContainer.addSubview(imageView)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果您希望图像为圆形:(并且没有边框显示)

let cornerRadius = imageWidth / 2
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Zay*_*ige 7

我在UIView上创建了一个帮手

@interface UIView (Helper)

- (void)roundCornerswithRadius:(float)cornerRadius
               andShadowOffset:(float)shadowOffset;
@end
Run Code Online (Sandbox Code Playgroud)

你可以这样称呼它

[self.view roundCornerswithRadius:5 andShadowOffset:5];
Run Code Online (Sandbox Code Playgroud)

这是实施

- (void)roundCornerswithRadius:(float)cornerRadius
               andShadowOffset:(float)shadowOffset
{
    const float CORNER_RADIUS = cornerRadius;
    const float SHADOW_OFFSET = shadowOffset;
    const float SHADOW_OPACITY = 0.5;
    const float SHADOW_RADIUS = 3.0;

    UIView *superView = self.superview;

    CGRect oldBackgroundFrame = self.frame;
    [self removeFromSuperview];

    CGRect frameForShadowView = CGRectMake(0, 0, oldBackgroundFrame.size.width, oldBackgroundFrame.size.height);
    UIView *shadowView = [[UIView alloc] initWithFrame:frameForShadowView];
    [shadowView.layer setShadowOpacity:SHADOW_OPACITY];
    [shadowView.layer setShadowRadius:SHADOW_RADIUS];
    [shadowView.layer setShadowOffset:CGSizeMake(SHADOW_OFFSET, SHADOW_OFFSET)];

    [self.layer setCornerRadius:CORNER_RADIUS];
    [self.layer setMasksToBounds:YES];

    [shadowView addSubview:self];
    [superView addSubview:shadowView];

}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个很好的优雅解决方案.确保您的视图在使用前已添加到其超级视图中.我添加了一些参数来让我更好地控制阴影,但整体效果很好.谢谢! (2认同)

Dha*_*ena 7

这是肯定有效的解决方案!

我已经创建了 UIView 扩展,其所需的边缘可以应用阴影,如下所示


enum AIEdge:Int {
    case
    Top,
    Left,
    Bottom,
    Right,
    Top_Left,
    Top_Right,
    Bottom_Left,
    Bottom_Right,
    All,
    None
}

extension UIView {
        
    func applyShadowWithCornerRadius(color:UIColor, opacity:Float, radius: CGFloat, edge:AIEdge, shadowSpace:CGFloat, cornerRadius: CGFloat)    {

        var sizeOffset:CGSize = CGSize.zero
        
        switch edge {
        case .Top:
            sizeOffset = CGSize(width: 0, height: -shadowSpace)
        case .Left:
            sizeOffset = CGSize(width: -shadowSpace, height: 0)
        case .Bottom:
            sizeOffset = CGSize(width: 0, height: shadowSpace)
        case .Right:
            sizeOffset = CGSize(width: shadowSpace, height: 0)
            
            
        case .Top_Left:
            sizeOffset = CGSize(width: -shadowSpace, height: -shadowSpace)
        case .Top_Right:
            sizeOffset = CGSize(width: shadowSpace, height: -shadowSpace)
        case .Bottom_Left:
            sizeOffset = CGSize(width: -shadowSpace, height: shadowSpace)
        case .Bottom_Right:
            sizeOffset = CGSize(width: shadowSpace, height: shadowSpace)
            
            
        case .All:
            sizeOffset = CGSize(width: 0, height: 0)
        case .None:
            sizeOffset = CGSize.zero
        }

        self.layer.cornerRadius = cornerRadius
        self.layer.masksToBounds = true

        self.layer.shadowColor = color.cgColor
        self.layer.shadowOpacity = opacity
        self.layer.shadowOffset = sizeOffset
        self.layer.shadowRadius = radius
        self.layer.masksToBounds = false

        self.layer.shadowPath = UIBezierPath(roundedRect:self.bounds, cornerRadius:self.layer.cornerRadius).cgPath
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,您可以为任何 UIView 子类调用阴影函数,如下所示,您还可以指定要应用阴影的边缘,根据需要更改以下方法调用的参数,尝试不同的变化。

viewRoundedToBeShadowedAsWell.applyShadowWithCornerRadius(color: .gray, opacity: 1, radius: 15, edge: AIEdge.All, shadowSpace: 15)

Run Code Online (Sandbox Code Playgroud)

注意:如果仍然不起作用,请尝试从主线程调用它

DispatchQueue.main.async {
   viewRoundedToBeShadowedAsWell.applyShadowWithCornerRadius(color: .gray, opacity: 1, radius: 15, edge: AIEdge.All, shadowSpace: 15)
}
Run Code Online (Sandbox Code Playgroud)

希望有人觉得这有用!

下面是结果图像:

在此输入图像描述

在此输入图像描述

在此输入图像描述


dGa*_*bit 6

在 swift 4 中快速测试的东西

import UIKit

extension UIView {
    @IBInspectable var dropShadow: Bool {
        set{
            if newValue {
                layer.shadowColor = UIColor.black.cgColor
                layer.shadowOpacity = 0.4
                layer.shadowRadius = 1
                layer.shadowOffset = CGSize.zero
            } else {
                layer.shadowColor = UIColor.clear.cgColor
                layer.shadowOpacity = 0
                layer.shadowRadius = 0
                layer.shadowOffset = CGSize.zero
            }
        }
        get {
            return layer.shadowOpacity > 0
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

生产

在此处输入图片说明

如果您在 Inspector 中启用它,如下所示:

在此处输入图片说明

它将添加用户定义的运行时属性,导致:

在此处输入图片说明

(我之前添加了cornerRadius = 8

:)


小智 5

经过一整天的阴影圆角视图研究,我很高兴在这里发布我的自定义uiview课程,希望结束这个问题:

RoundCornerShadowView.h

#import <UIKit/UIKit.h>

@interface RoundCornerShadowView : UIView

@end
Run Code Online (Sandbox Code Playgroud)

RoundCornerShadowView.m

#import "RoundCornerShadowView.h"

@implementation RoundCornerShadowView

// *** must override this method, not the other method ***
// otherwise, the background corner doesn't disappear....
// @2015/05/29
-(void) layoutSubviews {
    [super layoutSubviews];//is must to ensure rightly layout children view

    //1. first, create Inner layer with content
    CALayer *innerView = [CALayer layer];
    innerView.frame = CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height);
    //instead of: innerView.frame = self.frame;
    innerView.borderWidth = 1.0f;
    innerView.cornerRadius = 6.0f;
    innerView.masksToBounds = YES;
    innerView.borderColor = [[UIColor lightGrayColor] CGColor];
    innerView.backgroundColor = [[UIColor whiteColor] CGColor];
    //put the layer to the BOTTOM of layers is also a MUST step...
    //otherwise this layer will overlay the sub uiviews in current uiview...
    [self.layer insertSublayer:innerView atIndex:0];

    //2. then, create shadow with self layer
    self.layer.masksToBounds = NO;
    self.layer.shadowColor = [[UIColor darkGrayColor] CGColor];
    self.layer.shadowOpacity = 0.4f;
    //shadow length
    self.layer.shadowRadius = 2.0f;
    //no offset
    self.layer.shadowOffset = CGSizeMake(0, 0);
    //right down shadow
    //[self.layer setShadowOffset: CGSizeMake(1.0f, 1.0f)];

    //3. last but important, MUST clear current view background color, or the color will show in the corner!
    self.backgroundColor = [UIColor clearColor];
}

@end
Run Code Online (Sandbox Code Playgroud)

所以,无需在目标视图中添加视图或下方的子视图,只需在当前视图中添加一个图层,然后执行3步即可完成!

仔细查看代码中的注释,这有助于理解组件!


onm*_*133 5

您需要使用use shadowViewroundView

在此处输入图片说明

shadowView

  • 必须具有背景色
  • 应该落后 roundView
  • 诀窍是在shadowView内部进行一些布局,并且其阴影需要发光。调整insets使其shadowView完全不可见roundView

RoundView

  • 必须剪辑子视图

代码

addSubviews(shadowView, roundView)
roundView.addSubviews(titleLabel, subtitleLabel, imageView)

// need inset
shadowView.pinEdges(view: self, inset: UIEdgeInsets(constraintInsets: 2))
roundView.pinEdges(view: self)

do {
  shadowView.backgroundColor = .white // need background
  let layer = shadowView.layer
  layer.shadowColor = UIColor.black.cgColor
  layer.shadowRadius = 3
  layer.shadowOffset = CGSize(width: 3, height: 3)
  layer.shadowOpacity = 0.7
  layer.shouldRasterize = true
}

do {
  roundView.backgroundColor = .white
  let layer = roundView.layer
  layer.masksToBounds = true
  layer.cornerRadius = 5
}
Run Code Online (Sandbox Code Playgroud)

或者您可以在不指定的情况下执行以下操作 clipToBounds/maskToBounds

layer.shadowColor = UIColor.gray.cgColor
layer.shadowOffset = CGSize(width: 3, height: 3)
layer.shadowOpacity = 0.8
Run Code Online (Sandbox Code Playgroud)


Tho*_*mon 5

Swift 3 & IBInspectable 解决方案:
灵感来自 Ade 的解决方案

首先,创建一个 UIView 扩展:

//
//  UIView-Extension.swift
//  

import Foundation
import UIKit

@IBDesignable
extension UIView {
     // Shadow
     @IBInspectable var shadow: Bool {
          get {
               return layer.shadowOpacity > 0.0
          }
          set {
               if newValue == true {
                    self.addShadow()
               }
          }
     }

     fileprivate func addShadow(shadowColor: CGColor = UIColor.black.cgColor, shadowOffset: CGSize = CGSize(width: 3.0, height: 3.0), shadowOpacity: Float = 0.35, shadowRadius: CGFloat = 5.0) {
          let layer = self.layer
          layer.masksToBounds = false

          layer.shadowColor = shadowColor
          layer.shadowOffset = shadowOffset
          layer.shadowRadius = shadowRadius
          layer.shadowOpacity = shadowOpacity
          layer.shadowPath = UIBezierPath(roundedRect: layer.bounds, cornerRadius: layer.cornerRadius).cgPath

          let backgroundColor = self.backgroundColor?.cgColor
          self.backgroundColor = nil
          layer.backgroundColor =  backgroundColor
     }


     // Corner radius
     @IBInspectable var circle: Bool {
          get {
               return layer.cornerRadius == self.bounds.width*0.5
          }
          set {
               if newValue == true {
                    self.cornerRadius = self.bounds.width*0.5
               }
          }
     }

     @IBInspectable var cornerRadius: CGFloat {
          get {
               return self.layer.cornerRadius
          }

          set {
               self.layer.cornerRadius = newValue
          }
     }


     // Borders
     // Border width
     @IBInspectable
     public var borderWidth: CGFloat {
          set {
               layer.borderWidth = newValue
          }

          get {
               return layer.borderWidth
          }
     }

     // Border color
     @IBInspectable
     public var borderColor: UIColor? {
          set {
               layer.borderColor = newValue?.cgColor
          }

          get {
               if let borderColor = layer.borderColor {
                    return UIColor(cgColor: borderColor)
               }
               return nil
          }
     }
}
Run Code Online (Sandbox Code Playgroud)

然后,只需在界面构建器中选择您的 UIView 设置shadow ONcorner radius,如下所示:

选择你的 UIView

设置阴影开启和拐角半径

结果!

结果


归档时间:

查看次数:

253921 次

最近记录:

5 年,11 月 前