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

时间:2020-09-28 23:31:58

标签: ios swift xcode

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

有什么方法可以解决这个问题?这是bug还是发生了变化,我不知道?

这是我的代码示例。在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()
}

2 个答案:

答案 0 :(得分:5)

尽量不要直白地将内容直接添加到单元格的视图中。相反,应使用contentView将内容添加到单元格的contentView.addSubview()中。在iOS 14中,单元格的contentView出现在上方内容中,直接添加到该单元格中。实际上,这将禁止用户与这些项目进行互动,因为contentView将吸收所有互动。请参见下图:

在iOS 14之前的版本中,请注意添加到该单元格的所有视图后面的contentView pre iOS 14 content view

iOS 14及更高版本,请注意contentView位于直接添加到单元格的所有视图的前面

enter image description here

答案 1 :(得分:0)

按钮是否可见?

确保您使用了configureEditProfileButtonButton()函数并将“ editProfileButton”添加到主视图中。