gpt4 book ai didi

ios - 自定义按钮不响应触摸 - 快速

转载 作者:行者123 更新时间:2023-11-30 12:27:52 26 4
gpt4 key购买 nike

嗨,我有一个 ViewController,当点击我的 CollectionView 中的单元格时,它会弹出。我遇到的问题是 ViewController 顶部的按钮没有响应被点击。

弹出 View Controller 的代码:

import UIKit

class PopUpCellViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var expandedNoteTable: UITableView = UITableView()
let cellId = "cellId"
var picture: UIImage?
var comment: UILabel?

let addNBackgroundView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(r: 244, g: 244, b: 255)
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 8
view.layer.masksToBounds = true
return view
}()

lazy var addNButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Add N", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.isUserInteractionEnabled = true
button.setTitleColor(UIColor(r: 88, g: 88, b: 88), for: .normal)
button.titleLabel?.font = UIFont(name: "Nunito", size: 17)
button.addTarget(self, action: #selector(handleTouch), for: .touchUpInside)
return button
}()

let viewMBackgroundView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(r: 244, g: 244, b: 255)
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 8
view.layer.masksToBounds = true
return view
}()

lazy var viewMButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("M", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitleColor(UIColor(r: 88, g: 88, b: 88), for: .normal)
button.titleLabel?.font = UIFont(name: "Nunito", size: 17)
button.isUserInteractionEnabled = true
button.addTarget(self, action: #selector(handleTouch), for: .touchUpInside)
return button
}()

override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(viewMBackgroundView)
viewMBackgroundView.addSubview(viewMButton)
view.addSubview(addNBackgroundView)
addNBackgroundView.addSubview(addNButton)
view.addSubview(expandedNoteTable)

expandedNoteTable.frame = CGRect(x: 0, y: 67, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - (67+49))
expandedNoteTable.delegate = self
expandedNoteTable.dataSource = self
expandedNoteTable.separatorColor = UIColor.clear
expandedNoteTable.register(ExpandedNoteTableViewCell.self, forCellReuseIdentifier: "cellId")

self.view.backgroundColor = UIColor.white

setConstraints()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func handleTouch() {

print("button pressed")

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: cellId) as! ExpandedNoteTableViewCell
cell.imageView.image = picture
cell.commentLabel.text = comment?.text
return cell

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{

let pictureHeight = picture?.cgImage?.height

return CGFloat(pictureHeight! + 1000)
}



func setConstraints() {

viewMBackgroundView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -25).isActive = true
viewMBackgroundView.topAnchor.constraint(equalTo: view.topAnchor, constant: ((self.navigationController?.navigationBar.frame.size.height)!/2)).isActive = true
viewMBackgroundView.heightAnchor.constraint(equalToConstant: 35).isActive = true
viewMBackgroundView.widthAnchor.constraint(equalToConstant: 90).isActive = true

viewMButton.centerXAnchor.constraint(equalTo: viewMBackgroundView.centerXAnchor, constant: 10).isActive = true
viewMButton.centerYAnchor.constraint(equalTo: viewMBackgroundView.centerYAnchor).isActive = true

addNBackgroundView.rightAnchor.constraint(equalTo: viewMBackgroundView.leftAnchor, constant: -15).isActive = true
addNBackgroundView.centerYAnchor.constraint(equalTo: viewMBackgroundView.centerYAnchor).isActive = true
addNBackgroundView.heightAnchor.constraint(equalToConstant: 35).isActive = true
addNBackgroundView.widthAnchor.constraint(equalToConstant: 95).isActive = true

addNButton.centerXAnchor.constraint(equalTo: addNBackgroundView.centerXAnchor).isActive = true
addNButton.centerYAnchor.constraint(equalTo: addNBackgroundView.centerYAnchor).isActive = true
}

}

最佳答案

尝试下面的代码

    let button:UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
button.backgroundColor = .black
button.setTitle("Button", for: .normal)
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)

func buttonClicked() {
print("Button Clicked")
}

关于ios - 自定义按钮不响应触摸 - 快速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43948846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com