有没有办法制作一个完全透明的 UIButton ?

Is there a way to make a completely transparent UIButton that works?

我有一个自定义的 UINavigationItem 标题视图。它有一个标签被推到它的顶部,我已经启用它来响应 .touchDown 并发送一个动作。

但是,点击不会在标签顶部附近注册,可能是因为活动区域被剪裁了。所以我配置了另一个不可见视图(不在导航项中),并将其设置为控件,并将其定位在该导航标题视图标签上方。

但是,除非我将 'invisible' 视图的 alpha 设置为至少 0.02,否则它不起作用,因为 Apple 似乎故意禁用 alpha 小于该值的视图的操作。不幸的是,在深色模式下的黑屏上,'invisible' hitpad 视图显示为一个略带灰色的矩形,这不太美观。

我想我可以竭尽全力使按钮背景颜色与该位置的屏幕背景匹配,但它看起来有点俗气。

我还有哪些选择?

您可以简单地创建一个空白的 png 图像,并将其添加到标题视图的顶部。确保将 imageView 和标题视图 isUserInteractionEnabled 属性设置为 true:

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .green
        let imageView = UIImageView()
        imageView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(
            target: self,
            action: #selector(tap)
        )
        imageView.addGestureRecognizer(tap)
        imageView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        imageView.image = UIImage(named: "blank")
        let titleLabel = UILabel()
        titleLabel.text = "Transparent Button"
        titleLabel.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        titleLabel.addSubview(imageView)
        navigationItem.titleView = titleLabel
        navigationItem.titleView?.isUserInteractionEnabled = true
    }
    @objc func tap(_ gesture: UITapGestureRecognizer) {
        print(#function)
    }
}

Sample project


您也可以直接将手势识别器添加到您的 titleView。除非你需要控制手势的区域,否则根本不需要透明图像:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .green
        let tap = UITapGestureRecognizer(
            target: self,
            action: #selector(tap)
        )
        let titleLabel = UILabel()
        titleLabel.text = "Transparent Button"
        titleLabel.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        titleLabel.addGestureRecognizer(tap)
        navigationItem.titleView = titleLabel
        navigationItem.titleView?.isUserInteractionEnabled = true
    }
    @objc func tap(_ gesture: UITapGestureRecognizer) {
        print(#function)
    }
}

这是对@LeoDabus 已接受的有效答案的改编。然而,他的解释和例子完全告诉了他。我对 Leo 的示例所做的唯一有意义的更改是以编程方式创建 real 空图像,并删除标签生成。如果没有真正的空 UIImage(),在我发现的区域上进行点击的唯一方法是将图像视图的背景颜色设置为 non-clear。

func emptyImage(with size: CGSize) -> UIImage?
{
    UIGraphicsBeginImageContext(size)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

func configureButtons() {
    let imageView = UIImageView(image: emptyImage(with: CGSize(width: view.frame.size.width - 250, height: 44)))
    imageView.frame = CGRect(x: 140, y: self.view.safeAreaInsets.top + 50,
                             width: view.frame.size.width - 250, height: 44)
    imageView.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(actionEnableTitleEditing))
    imageView.addGestureRecognizer(tap)
    imageView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    view.addSubview(imageView)
}