以编程方式生成的 UIButtons 没有响应

Programmatically generated UIButtons not responsive

我正在动态生成 UIButton 并将它们放置在动态大小的 UIImageView 中。我在一个循环中创建了按钮,然后在另一个循环中我根据可用的 space 设置了它们的约束。一切正常,即按钮及其文本显示得很好,但单击它们时没有任何反应。 button.isUserinteractionIsEnables = 是。

当通过最终循环 I 记录按钮时,它们没有 Y、X 坐标。我认为我的主播会处理这个问题。或者将 CGRect 与锚点结合总是一个坏主意?

编辑:已修复 在按钮的 parentclass init 上,我必须设置 .isUserInteractionEnabled = true; 如果在任何时候,一个视图禁用了用户交互,那么它将拒绝执行其标准操作,包括不将事件传递给它的任何子视图

import UIKit

class SuggenstionCloud: UIImageView {

    override init(image: UIImage?) {
        super.init(image: image)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    public func setConstraints(
        topAnchor : NSLayoutAnchor<NSLayoutYAxisAnchor>, topConstant: CGFloat,
        bottomAnchor: NSLayoutAnchor<NSLayoutYAxisAnchor>, bottomConstant: CGFloat,
        trailingAnchor: NSLayoutAnchor<NSLayoutXAxisAnchor>, trailingConstant: CGFloat,
        leadingAnchor: NSLayoutAnchor<NSLayoutXAxisAnchor>, leadingConstant: CGFloat)
    {
        self.isUserInteractionEnabled = true
        self.contentMode = .scaleToFill
        self.topAnchor.constraint(equalTo: topAnchor, constant: topConstant).isActive = true;
        self.bottomAnchor.constraint(equalTo: bottomAnchor, constant: bottomConstant).isActive = true;
        self.trailingAnchor.constraint(equalTo: trailingAnchor, constant: trailingConstant).isActive = true;
        self.leadingAnchor.constraint(equalTo:  leadingAnchor, constant: leadingConstant).isActive = true;
    }

    public func setLabels(weightedTags: [String: Int], selectedTags: [String]) {
        let buttons : [UIButton] = createButtons(weightedTags: weightedTags);
        createLayout(buttons: buttons)
    }

    private func createButton(buttonText: String) -> UIButton {
        let button = UIButton()
        button.setTitle(buttonText, for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.titleLabel?.font = UIFont(name: "Avenir-Light", size: 20.0)
        button.translatesAutoresizingMaskIntoConstraints = false;
        self.addSubview(button)
        button.frame = CGRect(x:0,y:0,width:button.intrinsicContentSize.width, height: button.intrinsicContentSize.height)
        button.addTarget(self, action: #selector(self.onButtonPresed(_:)), for: .touchUpInside);
        return button;
    }

    @objc func onButtonPresed(_ sender: UIButton) {
        // guard let label = sender.titleLabel else {return}
        print("Button : \(sender) poressed")
    }

    private func createButtons(weightedTags: [String: Int]) -> [UIButton] {
        var buttons : [UIButton] = [];
        for tag in weightedTags {
            buttons.append(createButton(buttonText: tag.key))
        }
       return buttons;
    }


    private func createLayout(buttons : [UIButton]) {
        if buttons.count == 0 { return }
        let topPadding : CGFloat = 30;
        let sidePadding : CGFloat = 32;

        let padding : CGFloat = 10;
        let availableHeight : CGFloat = self.frame.height + (-2 * topPadding)
        let availableWidth : CGFloat = self.frame.width + (-2 * sidePadding)

        var i = 0;

        var totalHeight : CGFloat = topPadding
        var rowLength : CGFloat = 0;
        var rowCount : Int = 0;
        var lastButton : UIButton!


        for button in buttons {
            if totalHeight > availableHeight { print("Cloud out of space"); return }
            let buttonWidth = button.intrinsicContentSize.width;
            let buttonHeight = button.intrinsicContentSize.height;

            if rowLength == 0 && rowCount == 0
            {
                print("FirstButtonLabel  \(String(button.titleLabel!.text!))")
                setFirstButtonConstraint(button: button, totalHeight: totalHeight, sidePadding: sidePadding)
                rowLength += buttonWidth + sidePadding + 5; // FIX annoying first row image overlap
            }
            else if rowLength + buttonWidth + padding < availableWidth
            {
                setConstraint(button: button, lastButton: lastButton, totalHeight: totalHeight, padding: padding)
                rowLength += buttonWidth + padding;
            }
            else
            {
                print("Out of space")
                totalHeight += buttonHeight + padding
                rowLength = buttonWidth + sidePadding;
                rowCount += 1;
                setNewRowConstraint(button: button, totalHeight:totalHeight , sidePadding: sidePadding)
            }
            i += 1;
            lastButton = button
            print(button.isUserInteractionEnabled)
            print(button.allTargets)
            print(button)
        }
    }
    private func setFirstButtonConstraint(button: UIButton, totalHeight: CGFloat, sidePadding: CGFloat) {
        button.topAnchor.constraint(equalTo: self.topAnchor, constant: totalHeight).isActive = true;
        button.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: sidePadding + 5).isActive = true;
    }
    private func setConstraint(button: UIButton, lastButton: UIButton, totalHeight: CGFloat, padding:CGFloat) {
        button.leadingAnchor.constraint(equalTo: lastButton.trailingAnchor, constant: padding).isActive = true;
        button.topAnchor.constraint(equalTo: self.topAnchor, constant: totalHeight).isActive = true;
    }
    private func setNewRowConstraint(button: UIButton, totalHeight: CGFloat, sidePadding: CGFloat) {
        button.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: sidePadding).isActive = true;
        button.topAnchor.constraint(equalTo: self.topAnchor, constant: totalHeight).isActive = true;
    }
}

打印语句提供以下输出:

true
[AnyHashable(<unFatMobile.SuggenstionCloud: 0x7fb5fae028e0; baseClass = UIImageView; frame = (10 280; 375 317); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x6000005de920>>)]
<UIButton: 0x7fb5fac2d840; frame = (0 0; 41 40); opaque = NO; layer = <CALayer: 0x6000005d0fe0>> 

我想我可能在 class 生命周期方法上做错了什么。

我看到评论中已经回答了这个问题,但有一点背景知识。

当用户点击屏幕时,获取事件的是顶级视图。如果它已将 isUserInteractionEnabled 设置为 true,那么它会执行其标准操作(每个 UIView 可以在点击时执行不同的操作。)基本 UIView 的标准操作是确定在将事件传递给该子视图时点击了哪个子视图。通过这种方式,点击事件沿着视图的层次结构从最一般到最具体。

如果某个视图在任何时候禁用了用户交互,那么它将拒绝执行其标准操作,包括不将事件传递给它的任何子视图。

另一种意外使 UIButton 不可贴带的方法是将其绘制到其父视图的边界之外。视图仅将事件传递给在其边界内绘制的子视图。