1

I'm using Version 12.0.1 (12A7300). I have some buttons on UIViews and others in UITableViewCells. The actions for the buttons inside table cells are in the parent view controller which I access using protocols. When running the app either on simulators or on my device. All the buttons are added programmatically. The buttons do not work. I reinstalled Xcode Version 11.7 (11E801a) and everything works.

Any way to get around this issue? Is this a bug or did something change and I'm not aware of?

Here is an example of my code. In the 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()
}
GIJoeCodes
  • 1,680
  • 1
  • 25
  • 28
  • Just tested a fresh copy of Xcode 12. Buttons still do not work. Everything works when using Xcode 11.7. I'll wait until several updates of Xcode 12 have been released. – GIJoeCodes Sep 29 '20 at 01:35
  • 1
    How are you adding the buttons to table cells? with `addSubview` or with `contentView.addSubview`? – Sanzio Angeli Sep 29 '20 at 01:55
  • For this particular button I have a table view header `tableView.tableHeaderView = tableViewHeader` to which I add the button using addSubView as in `tableViewHeader.addSubview(editProfileButton)`. Other buttons I'm adding into a stackview and I'm using again addSubView to add the StackView to the tableviewcell. It didn't occur to me to adding them using contentView. I'll try that and see if that works. – GIJoeCodes Sep 29 '20 at 02:39

2 Answers2

5

As bluntly as possible you should not add content to a cell's view directly. Instead, content should be added to the cell's contentView by using contentView.addSubview(). In iOS 14, a cell's contentView appears above content added directly to the cell. This will in effect prohibit a user from interacting with those items, as the contentView will absorb all interaction. See the below images:

Pre iOS 14, notice the contentView behind all views added to the cell pre iOS 14 content view

iOS 14 and above, notice the contentView is in front of all views added directly to the cell

enter image description here

Sanzio Angeli
  • 2,872
  • 1
  • 16
  • 30
  • The question turns out to be about a header view, not a cell. Your guess is certainly right, but you should probably be talking about UITableViewHeaderFooterView, not UITableViewCell. – matt Sep 29 '20 at 03:31
  • Question states - "The actions for the buttons inside table cells are in the parent view controller which I access using protocols." The example provided code was from a header... maybe edit either the question wording or the provided code? Good learning opportunity for both me and GIJoeCodes either way. – Sanzio Angeli Sep 29 '20 at 03:57
  • 1
    I edited the code provided as it was the wrong code. Although I "WAS" having issues with both elements, I initially should have added the code shown. – GIJoeCodes Sep 29 '20 at 11:50
0

Do the buttons visible?

Make sure you used configureEditProfileButton() function and added "editProfileButton" to main view.

Tomer
  • 13
  • 2
  • The buttons are visible and as I mentioned, everything works in Xcode 11. I deleted Xcode 12 and I'm in the process of reinstalling it. Maybe a fresh version may work without issues. – GIJoeCodes Sep 29 '20 at 01:21