iOS14 应用程序没有响应按钮和一些用户交互

Buttons and some user interactions are not responding with iOS14 app

我的 iOS 应用遇到了一个奇怪的错误。当使用带有 iOS 13 的模拟器时,应用程序可以正常工作,但是当使用 iOS 14 时,按钮、开关和其他功能没有响应。控制台没有错误输出。 我不明白为什么这只发生在 XCode 11 和 iOS 14.

这是一个初始化我在 View 中的按钮之一的片段。

 let logButton: UIButton = {
    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setLogInButton()
    button.setTitle(NSLocalizedString("log_in", comment: ""), for: .normal)
    return 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)
    }

有动作

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

正如我所说,按钮(开关和其他)仅在 iOS 14 中没有响应。 看起来 targetActions 没有工作。

感谢您提供的任何帮助。 问候 马特

我遇到了同样的问题,table 单元格中的按钮不起作用

出于某种原因,您必须将按钮添加到单元格的 contentView 而不是单元格本身,如下所示

cell.contentView.addSubView(button)

之后为我工作

只需遵循此解决方案

对于单元格:

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)
    }
}

查看:

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
        }
    }
}