2017-05-13 9 views
0

こんにちは私は、私のcollectionview内のセルがタップされたときにポップアウトするViewControllerを持っています。私の問題は、ViewControllerの上部にあるボタンがタップされていることに応答していないことです。カスタムボタンがtouch-swiftに応答しない

ポップアウトビューコントローラのためのコード:

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

があなたのボタンのコードは、すべての罰金ですが、背後あなたnavigationControllerあなたのボタンを配置しているように見える

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

怒鳴るのコードを試してみてくださいのnavigationBar。私はあなたが他のどこかでNavigationBarをクリアするコードを持っていると仮定します。そうしないと、ボタンがまったく表示されません。

問題は、navigationBarがボタンに達する前に接触を傍受していることです。それを修正するには、(あなたがnavigationBarの透明度を設定したり、任意の場所)viewDidLoad()に次の行を追加します。

self.navigationController?.navigationBar.isUserInteractionEnabled = false 

これはタッチが下のボタンにそれを通過することを可能にする、バーのためにユーザーとの対話をオフにします。

関連する問題