子视图按钮不起​​作用

时间:2017-08-28 11:59:06

标签: ios swift uibutton uicontrolevents

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

5 个答案:

答案 0 :(得分:2)

我相信子视图的高度和宽度保持为0,因为按钮没有绑定到任何边缘,按钮似乎定义了超视图的高度。你总是可以通过设置clipToBounds = true来检查这个。如果你在视图中使用self,那么调用懒惰总是好的。

这可以解决您的问题:

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或anchorpoints。但我认为这应该是正确的。

答案 1 :(得分:1)

在Masterbed属性里面,把userinteractionenbaled = true,这将解决问题,其他的东西是addSubview ...把​​它放在subView Item或Class ..就像masterBed.addSubView(youurButton),如果你使用userinteration在按钮上..真的没有任何意义,因为按钮已经是为交互创建的对象..如果我们在U​​IImageView上放置一个按钮,我们需要在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")
}

我希望这可以帮助你并解决问题,欢呼!

答案 2 :(得分:0)

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

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

}

答案 3 :(得分:0)

可能是你的子视图不在父视图框架外,所以你可以尝试给父视图框架提供相同的框架并尝试这样:

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)

答案 4 :(得分:0)

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