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

时间:2018-12-31 12:35:00

标签: swift uibutton target programmatically generated

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

在通过最终循环I登录按钮时,它们没有Y,X坐标。我认为我的主播会照顾这个。还是将CGRect与锚点结合始终是个坏主意?

编辑:固定 在按钮的父类初始化中,我必须设置.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;
    }
}

Print语句提供以下输出:

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

我认为类生命周期方法可能做错了事。

1 个答案:

答案 0 :(得分:1)

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

当用户点击屏幕时,获取事件的是顶层视图。如果将isUserInteractionEnabled设置为true,则它将执行其标准动作(每个UIView可以在点击时执行其他操作。)基本的UIView的标准动作是确定将事件传递给该子视图时,点击了哪个子视图。这样,轻击事件就将视图层次结构从最一般的视图降到最具体的视图。

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

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