swift 子视图按钮不起作用

yftpprvb  于 2023-10-15  发布在  Swift
关注(0)|答案(5)|浏览(136)
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")
}
dwthyt8l

dwthyt8l1#

我相信子视图的高度和宽度仍然是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))
}

@objc func buttonAction() {
    //Do stuff
    }
}

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

6ie5vjzr

6ie5vjzr2#

我也有同样的问题。要解决这个问题,只需将isUserInteractionEnabled = true设置为PANView(添加按钮子视图的视图)

yk9xbfzb

yk9xbfzb3#

在Masterbed属性里面,输入userinteractionenbaled = true,这将解决问题,其他的东西是addSubview.把它放在子视图项或类上..比如masterBed.addSubView(youurButton),如果你在按钮上使用用户交互,真的没有意义,因为按钮已经是一个为交互创建的对象。这是同样的事情,如果我们把一个按钮放在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")
}

我希望这对你有帮助,解决这个问题,干杯!

jvlzgdj9

jvlzgdj94#

可能是你的子视图是出了父视图框架,所以你可以尝试给予相同的框架作为父视图框架,并尝试这样做:

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

mbzjlibv5#

我已经编辑了你的代码如下,它的工作。

您的ViewController类

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

}

HomeView类

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

}

相关问题