Buttons and some user interactions are not responding with iOS14 app

Viewed 1659

I am struggling with a strange bug with my iOS app. When using a simulator with iOS 13 application works as it should, but when using iOS 14 buttons, switches, and other functionalities are not responding. There is no error output in the console. I don't understand why this happens only with XCode 11 and iOS 14.

This a snippet with initializing one of my buttons in the View.

 let logButton: UIButton = {
    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setLogInButton()
    button.setTitle(NSLocalizedString("log_in", comment: ""), for: .normal)
    return button
}()

Here I'm assigning the target to the button.

   override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initConstraints()
        backgroundColor = UIColor().settingsTableViewCellColor()
        logButton.addTarget(self, action: #selector(buttonLogInLogOut), for: .touchUpInside)
    }

There is the action

@objc func buttonLogInLogOut(_ sender: UIButton){
    print("Log in clicked")
    delegate?.logInLogOutFunc(sender)
}

As I said the buttons (switches, and others) are not responding ONLY in iOS 14. It looks like targetActions are not working.

Thanks for any kind of help. Regards Matt

2 Answers

I had the same problem with a button in a table cell not working

For some reason you have to add the button to the contentView of the cell instead of the cell itself as follows

cell.contentView.addSubView(button)

Worked for me afterwards

Just follow this solution

For Cell:

class CommentCell: UICollectionViewCell {

    private let commentViewCell: CommentViewCell = {
        let view = CommentViewCell()
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(commentViewCell)
        
        NSLayoutConstraint.activate([
            commentViewCell.leadingAnchor.constraint(equalTo: leadingAnchor),
            commentViewCell.trailingAnchor.constraint(equalTo: trailingAnchor),
            commentViewCell.topAnchor.constraint(equalTo: topAnchor),
            commentViewCell.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setData(item: CommentViewModel) {
        commentViewCell.setData(item: item)
    }
}

For View:

class CommentViewCell: UIView {

    private let lblId: CustomClick = {
        let view = CustomClick()
        view.backgroundColor = .cyan
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    private let lblMessage: UILabel = {
        let view = UILabel()
        view.backgroundColor = .cyan
        view.textColor = .black
        view.font = .boldSystemFont(ofSize: 16)
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    private let lblDate: UILabel = {
        let view = UILabel()
        view.backgroundColor = .systemIndigo
        view.textColor = .black
        view.font = .boldSystemFont(ofSize: 16)
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .brown
        addSubview(lblId)
        addSubview(lblMessage)
        addSubview(lblDate)
        
        NSLayoutConstraint.activate([
            lblId.topAnchor.constraint(equalTo: topAnchor),
            lblId.trailingAnchor.constraint(equalTo: trailingAnchor),
            lblId.leadingAnchor.constraint(equalTo: leadingAnchor),
            
            lblMessage.trailingAnchor.constraint(equalTo: trailingAnchor),
            lblMessage.leadingAnchor.constraint(equalTo: leadingAnchor),
            lblMessage.topAnchor.constraint(equalTo: lblId.bottomAnchor),
            
            lblDate.trailingAnchor.constraint(equalTo: trailingAnchor),
            lblDate.leadingAnchor.constraint(equalTo: leadingAnchor),
            lblDate.topAnchor.constraint(equalTo: lblMessage.bottomAnchor),
            
        ])

        lblId.addTarget(self, action: #selector(onClick), for: .touchUpInside)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func onClick() {
        print("this is click button")
    }
    

    
    
    func setData(item: CommentViewModel) {
        lblId.setData(item: item.id)
        lblMessage.text = item.meesage
        lblDate.text = item.date
        lblDate.textColor = item.cellColor
    }
    
    class CustomClick: UIControl {
        
        private let lblId: UILabel = {
            let view = UILabel()
            view.textColor = .black
            view.backgroundColor = .systemPink
            view.font = .boldSystemFont(ofSize: 16)
            view.translatesAutoresizingMaskIntoConstraints = false
            return view
        }()
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            addSubview(lblId)
            NSLayoutConstraint.activate([
                lblId.topAnchor.constraint(equalTo: topAnchor),
                lblId.trailingAnchor.constraint(equalTo: trailingAnchor),
                lblId.leadingAnchor.constraint(equalTo: leadingAnchor),
                lblId.bottomAnchor.constraint(equalTo: bottomAnchor)
            ])
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        func setData(item: String) {
            lblId.text = item
        }
    }
}
Related