我正在try 创建一个具有rounded cornersdrop shadow的按钮.不管我怎么打开,按钮都不能正确显示.我试过masksToBounds = falsemasksToBounds = true,但要么是角半径起作用而阴影不起作用,要么是阴影起作用而角半径不能修剪按钮的角.

import UIKit
import QuartzCore

@IBDesignable
class Button : UIButton
{
    @IBInspectable var masksToBounds: Bool    = false                {didSet{updateLayerProperties()}}
    @IBInspectable var cornerRadius : CGFloat = 0                    {didSet{updateLayerProperties()}}
    @IBInspectable var borderWidth  : CGFloat = 0                    {didSet{updateLayerProperties()}}
    @IBInspectable var borderColor  : UIColor = UIColor.clearColor() {didSet{updateLayerProperties()}}
    @IBInspectable var shadowColor  : UIColor = UIColor.clearColor() {didSet{updateLayerProperties()}}
    @IBInspectable var shadowOpacity: CGFloat = 0                    {didSet{updateLayerProperties()}}
    @IBInspectable var shadowRadius : CGFloat = 0                    {didSet{updateLayerProperties()}}
    @IBInspectable var shadowOffset : CGSize  = CGSizeMake(0, 0)     {didSet{updateLayerProperties()}}

    override func drawRect(rect: CGRect)
    {
        updateLayerProperties()
    }

    func updateLayerProperties()
    {
        self.layer.masksToBounds = masksToBounds
        self.layer.cornerRadius = cornerRadius
        self.layer.borderWidth = borderWidth
        self.layer.borderColor = borderColor.CGColor
        self.layer.shadowColor = shadowColor.CGColor
        self.layer.shadowOpacity = CFloat(shadowOpacity)
        self.layer.shadowRadius = shadowRadius
        self.layer.shadowOffset = shadowOffset
    }
}

推荐答案

以下SWIFT 5/IOS 12代码显示了如何设置一个UIButton的子类,以允许创建具有圆角和阴影的实例:

import UIKit

final class CustomButton: UIButton {

    private var shadowLayer: CAShapeLayer!

    override func layoutSubviews() {
        super.layoutSubviews()

        if shadowLayer == nil {
            shadowLayer = CAShapeLayer()
            shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 12).cgPath
            shadowLayer.fillColor = UIColor.white.cgColor

            shadowLayer.shadowColor = UIColor.darkGray.cgColor
            shadowLayer.shadowPath = shadowLayer.path
            shadowLayer.shadowOffset = CGSize(width: 2.0, height: 2.0)
            shadowLayer.shadowOpacity = 0.8
            shadowLayer.shadowRadius = 2

            layer.insertSublayer(shadowLayer, at: 0)
            //layer.insertSublayer(shadowLayer, below: nil) // also works
        }        
    }

}

根据需要,您可以在故事板中添加UIButton并将其类设置为CustomButton,也可以通过编程方式创建CustomButton的实例.以下UIViewController实现展示了如何以编程方式创建和使用CustomButton实例:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let button = CustomButton(type: .system)
        button.setTitle("Button", for: .normal)
        view.addSubview(button)

        button.translatesAutoresizingMaskIntoConstraints = false
        let horizontalConstraint = button.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        let verticalConstraint = button.centerYAnchor.constraint(equalTo: view.centerYAnchor)        
        let widthConstraint = button.widthAnchor.constraint(equalToConstant: 100)
        let heightConstraint = button.heightAnchor.constraint(equalToConstant: 100)
        NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint])
    }

}

前面的代码在iPhone模拟器中生成以下图像:

enter image description here

Ios相关问答推荐

SWIFT AVFoundation翻转相机功能不起作用

UITableViewCell编程约束问题

一堆UIGraphics方法在iOS 17中被弃用,没有明确的替代方法?

OnTapGesture在其修改的视图外部触发

执行devicectl:ProcessException时出错:进程异常退出:错误:命令超时超过5.0秒

flutter中实现自定义底部导航栏

如何在@AppStorage中存储字体?

AVAudioRecord 没有音频

Swift中是否有一种方法可以为可选属性创建一个计算(computed)属性

具有一个参数/参数的泛型类的泛型类

如何在 Mac OS Ventura 中使用 Xcode 13

FileHandle 不会在 iOS 中释放内存

弹出视图后使所有背景 UI 模糊

通过 tuist/XcodeProj 以编程方式将文件添加到本机目标

Swift枚举继承

如何使用 swift 添加速率按钮的链接?

以编程方式创建按钮并设置背景图像

如果已安装,则重定向到应用程序,否则重定向到 App Store

ARC - __unsafe_unretained 的含义?

在两个或多个 iPhone 应用程序之间共享数据