Swift iOS-如何在单元格的SuperView中获取按钮框架

时间:2018-07-12 13:20:00

标签: ios swift uicollectionviewcell cgrect superview

我在视图控制器中有collectionView。在collectionView单元格中,我有一个按钮。由于将有几个单元格,每个按钮在vc内将具有不同的位置。 点击按钮时,如何在ViewController(而不是collectionView)内部获取按钮的框架?我想从单元格内部而不是cellForItem中获取按钮。

无论我在任何单元格内点击哪个按钮,我都在下面尝试了此操作,但它始终会打印出(0.0, 64.0, 0.0, 0.0)

@objc func myButtonTapped() {
    let locationInSuperView = self.convert(myButton.frame, to: nil)
    print(locationInSuperView)
}

以下代码:

CollectionViewCell:

class MyCell: UICollectionViewCell{

    lazy var myButton: UIButton = {
        let button = UIButton(type: .system)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setImage(UIImage(named: "myButton"), for: .normal)
        button.addTarget(self, action: #selector(myButtonTapped), for: .touchUpInside)
        return button
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .white

        contentView.addSubview(myButton)
        myButton.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -8).isActive = true
        myButton.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
        myButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
        myButton.widthAnchor.constraint(equalToConstant: 50).isActive = true
    }

    @objc func myButtonTapped() {

        let locationInSuperView = self.superview!.convert(myButton.frame, to: nil)
        print(locationInSuperView)
    }
}

ViewController:

ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    var collectionView: UICollectionView!
    let myCell = "myCell"

    override func viewDidLoad() {
        super.viewDidLoad()

        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
        layout.scrollDirection = .vertical

        collectionView = UICollectionView(frame: view.frame, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(MyCell.self, forCellWithReuseIdentifier: myCell)
        view.addSubview(collectionView)
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: myCell, for: indexPath) as! MyCell

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: 50)
    }
}

2 个答案:

答案 0 :(得分:2)

cellForItemAt的{​​{1}}内尝试此代码

UICollectionView

答案 1 :(得分:1)

在转换中使用nil引起了问题。我使用了应用程序的窗口,并将其从无切换为window,问题已解决。

@objc func myButtonTapped() {

    if let window = UIApplication.shared.keyWindow {

        let locationInWindow = self.convert(myButton.frame, to: window)
        print(locationInWindow)
    }
}