swift 子视图按钮不起作用

yftpprvb  于 2023-10-15  发布在  Swift
关注(0)|答案(5)|浏览(174)
  1. masterBtn.addTarget(self, action: #selector(self.masterBed), for: UIControlEvents.touchUpInside)

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

  1. let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
  2. button.setTitleColor(.gray, for: .normal)
  3. button.center = CGPoint(x: 380, y: 110)
  4. button.setTitle(">", for: .normal)
  5. button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
  6. self.addSubview(button)
  7. func buttonAction () {
  8. print("button pressed")
  9. }
dwthyt8l

dwthyt8l1#

我相信子视图的高度和宽度仍然是0,因为按钮没有绑定到任何边缘,按钮似乎定义了它的超级视图的高度。你可以通过设置clipToBounds = true来检查。如果在视图中使用self,调用lazy总是好的。
这可以解决你的问题:

  1. class buttonView: UIView {
  2. private lazy var button: UIButton = {
  3. let button = UIButton()
  4. button.setTitleColor(.gray, for: .normal)
  5. button.setTitle("MyButton", for: .normal)
  6. button.translatesAutoresizingMaskIntoConstraints = false
  7. button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
  8. return button
  9. }()
  10. override init(frame: CGRect) {
  11. super.init(frame: frame)
  12. setup()
  13. }
  14. required init?(coder aDecoder: NSCoder) {
  15. super.init(coder: aDecoder)
  16. setup()
  17. }
  18. func setup() {
  19. addSubview(button)
  20. addConstraint(NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 10))
  21. addConstraint(NSLayoutConstraint(item: button, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 10))
  22. addConstraint(NSLayoutConstraint(item: button, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0))
  23. addConstraint(NSLayoutConstraint(item: button, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0))
  24. addConstraint(NSLayoutConstraint(item: button, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 80))
  25. addConstraint(NSLayoutConstraint(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
  26. }
  27. @objc func buttonAction() {
  28. //Do stuff
  29. }
  30. }

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

展开查看全部
6ie5vjzr

6ie5vjzr2#

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

yk9xbfzb

yk9xbfzb3#

在Masterbed属性里面,输入userinteractionenbaled = true,这将解决问题,其他的东西是addSubview.把它放在子视图项或类上..比如masterBed.addSubView(youurButton),如果你在按钮上使用用户交互,真的没有意义,因为按钮已经是一个为交互创建的对象。这是同样的事情,如果我们把一个按钮放在UIImageView上,我们需要在UIImageView上的属性上激活,以与按钮交互。就像这个样品一样。

  1. class ViewController: UIViewController {
  2. let myImage: UIImageView = {
  3. let image = UIImageView()
  4. image.backgroundColor = UIColor.brown
  5. image.translatesAutoresizingMaskIntoConstraints = false
  6. image.isUserInteractionEnabled = true // here activate the interaction needed
  7. image.image = UIImage(imageLiteralResourceName: "backgroundSE.jpg")
  8. return image
  9. }()
  10. let textLabel: UILabel = {
  11. let label = UILabel()
  12. label.text = "My App"
  13. label.textColor = UIColor.white
  14. label.font = UIFont.boldSystemFont(ofSize: 25)
  15. label.textAlignment = .center
  16. label.translatesAutoresizingMaskIntoConstraints = false
  17. return label
  18. }()
  19. lazy var sendMailButton: UIButton = {
  20. let button = UIButton(type: .system)
  21. button.setTitle("Send Mail", for: .normal)
  22. button.setTitleColor(.white, for: .normal)
  23. button.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
  24. button.translatesAutoresizingMaskIntoConstraints = false
  25. button.layer.cornerRadius = 14
  26. button.addTarget(self, action: #selector(handleSendMail), for: .touchUpInside)
  27. return button
  28. }()
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. view.addSubview(myImage)
  32. myImage.addSubview(sendMailButton)
  33. myImage.addSubview(textLabel)
  34. setupLayouts()
  35. }
  36. @objc func handleSendMail() {
  37. print("Mail Sended")
  38. }

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

展开查看全部
jvlzgdj9

jvlzgdj94#

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

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

mbzjlibv5#

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

您的ViewController类

  1. class ViewController: UIViewController, HomeViewDelegate {
  2. override func viewDidLoad() {
  3. super.viewDidLoad()
  4. middleView()
  5. }
  6. func middleView() {
  7. let customView = HomeView(frame: CGRect(x: 60, y: 100, width: 250, height: 100))
  8. self.view.addSubview(customView)
  9. customView.backgroundColor = UIColor.green
  10. customView.delegate = self
  11. }
  12. func buttonAclicked(_ sender: UIButton) {
  13. print("button click action in viewController")
  14. }
  15. }

HomeView类

  1. import UIKit
  2. protocol HomeViewDelegate {
  3. func buttonAclicked(_ sender: UIButton)
  4. }
  5. class HomeView: UIView {
  6. var delegate:HomeViewDelegate?
  7. override init(frame: CGRect) {
  8. super.init(frame: frame)
  9. initView()
  10. }
  11. required init?(coder aDecoder: NSCoder) {
  12. super.init(coder: aDecoder)
  13. }
  14. func initView() {
  15. let button = UIButton(frame: CGRect(x: 10, y: 10, width: 80, height: 35))
  16. self.addSubview(button)
  17. button.setTitleColor(.gray, for: .normal)
  18. button.setTitle("MyButton", for: .normal)
  19. button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
  20. }
  21. func buttonAction(_ sender: UIButton) {
  22. print("button pressed")
  23. delegate?.buttonAclicked(sender)
  24. }
  25. }
展开查看全部

相关问题