嗨我有一个ViewController,当我的collectionview中的一个单元格被点击时会弹出。我遇到的问题是我的ViewController顶部的按钮没有响应被点击。
弹出视图控制器的代码:
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
}
}
答案 0 :(得分:0)
尝试下面的代码
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")
}
答案 1 :(得分:0)
您的按钮代码一切正常,但看起来您将按钮放在后面的navigationController
navigationBar
。我假设您在其他地方有代码使导航栏清晰,否则您根本看不到按钮。
问题是:navigationBar
在到达按钮之前会拦截触摸。要解决此问题,请在viewDidLoad()
中(或在您设置navigationBar透明度的任何位置)添加此行:
self.navigationController?.navigationBar.isUserInteractionEnabled = false
这会关闭条形图的用户交互,允许触摸将其传递到下方的按钮。