在 Xcode 12 上运行应用程序时按钮不起作用

Jos*_*llo 1 xcode ios swift

我正在使用版本 12.0.1 (12A7300)。我在 UIViews 上有一些按钮,在 UITableViewCells 中有其他按钮。表格单元格内按钮的操作位于我使用协议访问的父视图控制器中。在模拟器或我的设备上运行应用程序时。所有按钮都是以编程方式添加的。按钮不起作用。我重新安装了 Xcode 版本 11.7 (11E801a),一切正常。

有什么办法可以解决这个问题?这是一个错误还是做了一些改变而我不知道?

这是我的代码示例。在 UITableViewCell 中:

var addToCartDelegate: AddToCartDelegate?

let addToCartButton: UIButton = {
    let button = UIButton()
    return button
}()

let applePayButton: PKPaymentButton = {
    let button = PKPaymentButton(paymentButtonType: .buy, paymentButtonStyle: .whiteOutline)
    return button
}()

func configureAddToCartButton() {
    let button = addToCartButton
    button.setTitle("Add To Cart", for: .normal)
    button.titleLabel?.font = getScaledFont(forFont: Fonts.bold, textStyle: .body)
    button.setTitleColor(Colors.primaryButtonTintColor, for: .normal)
    button.tintColor = Colors.primaryButtonTintColor
    button.backgroundColor = Colors.primaryButtonBackgroundColor
    button.layer.cornerRadius = Attributes.cornerRadius
    button.addTarget(self, action: #selector(addToCartButtonWasTapped), for: .touchUpInside)
}

func configureStackView() {
    
    var stackView = UIStackView()
    
    if useApplePay {
        stackView = UIStackView(arrangedSubviews: [addToCartButton, applePayButton])
    } else {
        stackView = UIStackView(arrangedSubviews: [addToCartButton])
    }
    
    stackView.axis = .horizontal
    stackView.spacing = 16
    stackView.distribution = .fillEqually
    addSubview(stackView)
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
    stackView.heightAnchor.constraint(equalToConstant: 44).isActive = true
    stackView.rightAnchor.constraint(equalTo: rightAnchor, constant: -16).isActive = true
    stackView.leftAnchor.constraint(equalTo: leftAnchor, constant: 16).isActive = true
}

@objc func addToCartButtonWasTapped() {
    addToCartDelegate?.addItemToCart()
}

@objc func applePayButtonWasTapped() {
    addToCartDelegate?.buyWithApplePay()
}
Run Code Online (Sandbox Code Playgroud)

San*_*eli 5

尽可能坦率地说,您不应该直接将内容添加到单元格的视图中。相反,含量应加入到细胞的contentView使用contentView.addSubview()。在 iOS 14 中,单元格contentView出现直接添加到单元格的内容上方。这实际上将禁止用户与这些项目交互,因为它们contentView会吸收所有交互。请参阅以下图片:

在 iOS 14 之前,注意contentView添加到单元格的所有视图的后面 iOS 14 之前的内容视图

iOS 14 及更高版本,注意contentView是在所有直接添加到单元格的视图之前

在此处输入图片说明