Метка центра на основе вида - PullRequest
1 голос
/ 12 марта 2019

Я хочу центрировать метку на основе вида. Метка находится внутри collectionView, и я хочу центрировать эту метку на основе представления, NOT внутри collectionView в время выполнения .

Я пытался изменить constant ограничения метки на середину кадра, но это не работает:

// THIS DOES NOT WORK. I want the `textLabel` centered inside the `yellowView`.
cell.textLabelCenterYVerticalConstraint.constant = yellowView.frame.midY

Вот экран:

enter image description here

Вот раскадровка:

enter image description here

Это моя коллекцияПросмотреть код

class ViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var yellowView: UIView!

    let imageName = ["1", "2", "3"]

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 5)
    }
}

// MARK: - UICollectionViewDataSource

extension ViewController: UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as! MyCollectionViewCell
        let index = indexPath.row
        let images = imageName[index]
        cell.backgroundImageView.image = UIImage(named: images)

        // THIS DOES NOT WORK. I want the `textLabel` centered inside the `yellowView`.
        cell.textLabelCenterYVerticalConstraint.constant = yellowView.frame.midY

        return cell
    }
}

// MARK: - UICollectionViewDelegateFlowLayout

extension ViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.bounds.width, height: collectionView.bounds.height)

    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0.0, left: 5.0, bottom: 0.0, right: 5.0)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10.0
    }
}

Это мой код сотовой связи

class MyCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var backgroundImageView: UIImageView!
    @IBOutlet weak var textLabel: UILabel!
    @IBOutlet weak var textLabelCenterYVerticalConstraint: NSLayoutConstraint!
}

См. Комментарий в коде // THIS DOES NOT WORK. Есть предложения?

...