子视图按钮不起​​作用

Subview button is not working

masterBtn.addTarget(self, action: #selector(self.masterBed), for: UIControlEvents.touchUpInside)

我在子视图中使用了上面的代码,但它没有触发函数 masterBed。 子视图中的按钮不可点击

完整代码:

let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.setTitleColor(.gray, for: .normal)
button.center = CGPoint(x: 380, y: 110)
button.setTitle(">", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.addSubview(button)

func buttonAction () {
    print("button pressed")
}

我已经按如下方式编辑了您的代码,它正在运行。

你的ViewControllerClass

class ViewController: UIViewController, HomeViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        middleView()
    }

    func middleView() {
        let customView = HomeView(frame: CGRect(x: 60, y: 100, width: 250, height: 100))
        self.view.addSubview(customView)
        customView.backgroundColor = UIColor.green
        customView.delegate = self
    }

    func buttonAclicked(_ sender: UIButton) {
        print("button click action in viewController")
    }

}

主页视图Class

import UIKit

protocol HomeViewDelegate {

    func buttonAclicked(_ sender: UIButton)
}

class HomeView: UIView {

    var delegate:HomeViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        initView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    func initView() {
        let button = UIButton(frame: CGRect(x: 10, y: 10, width: 80, height: 35))
        self.addSubview(button)
        button.setTitleColor(.gray, for: .normal)
        button.setTitle("MyButton", for: .normal)
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    }

    func buttonAction(_ sender: UIButton) {
        print("button pressed")
        delegate?.buttonAclicked(sender)
    }

}

可能是您的子视图超出了父视图框架,所以您可以尝试提供与父视图框架相同的框架并尝试这样:

let button = UIButton(frame: self.frame)//Parent view frame
button.setTitleColor(.gray, for: .normal)
//button.center = CGPoint(x: 380, y: 110) // comment this line 
button.setTitle(">", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.addSubview(button)

我相信子视图的高度和宽度保持为 0,因为按钮没有绑定到任何边缘并且按钮似乎定义了其父视图的高度。您始终可以通过设置 clipToBounds = true 来检查这一点。如果您在视图中使用 self,那么调用 lazy 总是好的。

这应该可以解决您的问题:

class buttonView: UIView {
private lazy var button: UIButton = {
    let button = UIButton()
    button.setTitleColor(.gray, for: .normal)
    button.setTitle("MyButton", for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    return button
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    setup()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setup()
}

func setup() {
    addSubview(button)

    addConstraint(NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 10))
    addConstraint(NSLayoutConstraint(item: button, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 10))
    addConstraint(NSLayoutConstraint(item: button, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: button, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: button, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 80))
    addConstraint(NSLayoutConstraint(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
}

func buttonAction() {
    //Do stuff
    }
}

有点不确定 NSLayoutConstraints,因为我使用 SnapKit 或锚点。但我认为这应该是正确的。

在 Masterbed 里面 属性,设置 userinteractionenbaled = true,这将解决问题,其他的东西是 addSubview...把它放在 subView Item 或 Class.. 像 masterBed.addSubView(youurButton) 如果你在按钮上使用 userinteration.. 真的没有意义,因为按钮已经是为交互创建的对象.. 如果我们在 UIImageView 上放置一个按钮,我们需要在属性 在 UIImageView 上,与按钮交互。喜欢这个样本..

  class ViewController: UIViewController {

 let myImage: UIImageView = {
    let image = UIImageView()
    image.backgroundColor = UIColor.brown
    image.translatesAutoresizingMaskIntoConstraints = false
    image.isUserInteractionEnabled = true    // here activate the interaction needed
    image.image = UIImage(imageLiteralResourceName: "backgroundSE.jpg")
    return image
}()

let textLabel: UILabel = {
   let label = UILabel()
    label.text = "My App"
    label.textColor = UIColor.white
    label.font = UIFont.boldSystemFont(ofSize: 25)
    label.textAlignment = .center
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

lazy var sendMailButton: UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("Send Mail", for: .normal)
    button.setTitleColor(.white, for: .normal)
    button.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.layer.cornerRadius = 14
    button.addTarget(self, action: #selector(handleSendMail), for: .touchUpInside)
    return button
}()

override func viewDidLoad() {
    super.viewDidLoad()


    view.addSubview(myImage)
    myImage.addSubview(sendMailButton)
    myImage.addSubview(textLabel)

    setupLayouts()

}

@objc func handleSendMail() {
    print("Mail Sended")
}

希望对您有所帮助,解决问题,干杯!

我有同样的问题。要解决它,只需将 isUserInteractionEnabled = true 设置为父视图(视图是您添加按钮子视图)