Ограничения VisualFormat перепутывают ограничения в других ячейках UICollectionView - PullRequest
0 голосов
/ 07 февраля 2019

Я пытаюсь реализовать текстовое представление, которое динамически меняет свою высоту с текстом, который он передает.Я нашел, как это сделать, но при этом я должен использовать ограничения VisualFormat.При реализации ограничений VisualFormat ограничения, установленные вне текущей ячейки, искажаются и возвращаются к параметрам по умолчанию (origin @ (0, 0) и т. Д.).Мне интересно, что я делаю не так, потому что из того, что я вижу, я делаю вещи по книге.Пожалуйста, дайте мне знать, спасибо.

import UIKit

class PostDetailController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    private let headerId = "headerId"
    private let infoId = "infoId"
    private let textId = "textId"

    var post: Post? {
        didSet {
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.backgroundView = backView
        collectionView?.contentInsetAdjustmentBehavior = .never
        collectionView?.translatesAutoresizingMaskIntoConstraints = false

        collectionView?.register(PostDetailHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerId)
        collectionView?.register(PostDetailInfomation.self, forCellWithReuseIdentifier: infoId)
        collectionView?.register(PostDetailText.self, forCellWithReuseIdentifier: textId)

        let layout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout
        layout?.minimumLineSpacing = 0

    }

    let backView: UIImageView = {
        let iv = UIImageView()
        iv.image = UIImage(named: "BackgroundGradient")
        iv.contentMode = .scaleAspectFill
        return iv
    }()

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: view.frame.width, height: 220)
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.item == 1 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: textId, for: indexPath) as! PostDetailText
            cell.textView.attributedText = textAttributedText()
            return cell
        }
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: infoId, for: indexPath) as! PostDetailInfomation
        cell.post = post
        return cell
    }

    private func textAttributedText() -> NSAttributedString {
        let attributedText = NSMutableAttributedString(string: "", attributes: [NSAttributedStringKey.font: UIFont(name: "Avenir-Medium", size: 14)!])
        if let text = post?.dummyText {
            attributedText.append(NSAttributedString(string: text, attributes: [NSAttributedStringKey.font: UIFont(name: "Avenir-Medium", size: 14)!, NSAttributedStringKey.foregroundColor: UIColor(red: 104.0/255.0, green: 104.0/255.0, blue: 104.0/255.0, alpha: 1.0)]))
        }
        return attributedText
    }

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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.item == 1 {
            let dummySize = CGSize(width: view.frame.width - 34, height: 1000)
            let options = NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin)
            let rect = textAttributedText().boundingRect(with: dummySize, options: options, context: nil)
            return CGSize(width: view.frame.width, height: rect.height + 100)
        }
        return CGSize(width: view.frame.width, height: 167)
    }

    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) as! PostDetailHeader
        return header
    }

}

class PostDetailHeader: BaseCell {

    override func setupViews() {
        super.setupViews()
        backgroundColor = UIColor.clear
    }
}

class PostDetailInfomation: BaseCell {

    var post: Post? {
        didSet {

            if let imageName = post?.companyNameLogo {
                companyNameLogo.image = UIImage(named: imageName)
            }
            if let title = post?.title {
                titleLabel.text = title
            }
            if let detail = post?.detail {
                detailLabel.text = detail
            }
            if let imageName = post?.profileImage {
                profileImageView.image = UIImage(named: imageName)
            }
            if let name = post?.name {
                nameLabel.text = "By: " + name
            }
            if let time = post?.time {
                timeLabel.text = time
            }
            if let text = post?.dummyText {
                textView.text = text
            }
            if let hashtag = post?.hash1 {
                hashButton1.setTitle(hashtag, for: .normal)
            }
            if let hashtag = post?.hash2 {
                hashButton2.setTitle(hashtag, for: .normal)
            }
        }
    }

    override func setupViews() {
        super.setupViews()

        backgroundColor = UIColor.white

        let margin = 17 as CGFloat
        let hashButtonHeight = 25 as CGFloat

        companyNameLogo.translatesAutoresizingMaskIntoConstraints = false
        addSubview(companyNameLogo)
        companyNameLogo.frame = CGRect(x: 15, y: 27, width: 134, height: 25)

        hashButton1.translatesAutoresizingMaskIntoConstraints = false
        addSubview(hashButton1)
        hashButton1.frame = CGRect(x: 181, y: 27, width: 100, height: hashButtonHeight)
        hashButton1.layer.cornerRadius = hashButton1.frame.height/2
        hashButton1.layer.masksToBounds = true

        hashButton2.translatesAutoresizingMaskIntoConstraints = false
        addSubview(hashButton2)
        hashButton2.frame = CGRect(x: hashButton1.frame.maxX + 2, y: hashButton1.frame.minY, width: 75, height: hashButtonHeight)
        hashButton2.layer.cornerRadius = hashButton2.frame.height/2
        hashButton2.layer.masksToBounds = true

        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        addSubview(titleLabel)
        titleLabel.frame = CGRect(x: margin, y: companyNameLogo.frame.maxY + 17, width: 269, height: 30)

        detailLabel.translatesAutoresizingMaskIntoConstraints = false
        addSubview(detailLabel)
        detailLabel.frame = CGRect(x: margin, y: titleLabel.frame.maxY + 8, width: 304, height: 22)

        profileImageView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(profileImageView)
        profileImageView.frame = CGRect(x: margin, y: detailLabel.frame.maxY + 8, width: 30, height: 30)
        profileImageView.layer.cornerRadius = profileImageView.frame.width/2
        profileImageView.layer.masksToBounds = true

        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        addSubview(nameLabel)
        nameLabel.frame = CGRect(x: profileImageView.frame.maxX + 15, y: profileImageView.frame.minY + 6, width: 200, height: 19)

        timeLabel.translatesAutoresizingMaskIntoConstraints = false
        addSubview(timeLabel)
        timeLabel.frame = CGRect(x: nameLabel.frame.maxX, y: profileImageView.frame.minY + 6, width: contentView.frame.width - nameLabel.frame.maxX - 17, height: 19)
    }

    let companyNameLogo: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFill
        return iv
    }()

    let titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(name: "Avenir-Heavy", size: 22)
        label.textColor = UIColor.black
        label.textAlignment = .left
        return label
    }()

    let detailLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(name: "Avenir-Medium", size: 16)
        label.textColor = UIColor(red: 171.0/255.0, green: 171.0/255.0, blue: 171.0/255.0, alpha: 1.0)
        label.textAlignment = .left
        return label
    }()

    let profileImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFill
        return iv
    }()

    let nameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(name: "Avenir-Medium", size: 14)
        label.textColor = UIColor(red: 104.0/255.0, green: 104.0/255.0, blue: 104.0/255.0, alpha: 1.0)
        label.textAlignment = .left
        return label
    }()

    let timeLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(name: "Avenir-Medium", size: 14)
        label.textColor = UIColor(red: 194.0/255.0, green: 194.0/255.0, blue: 194.0/255.0, alpha: 1.0)
        label.textAlignment = .right
        return label
    }()

    let textView: UILabel = {
        let label = UILabel()
        label.font = UIFont(name: "Avenir-Medium", size: 14)
        label.textColor = UIColor(red: 104.0/255.0, green: 104.0/255.0, blue: 104.0/255.0, alpha: 1.0)
        label.textAlignment = .left
        label.sizeToFit()
        label.numberOfLines = 0
        return label
    }()

    let hashButton1: UIButton = {
        let button = UIButton()
        button.titleLabel?.font = UIFont(name: "Avenir-Medium", size: 13)
        button.setTitleColor(UIColor.black, for: .normal)
        button.titleEdgeInsets = UIEdgeInsets(top: 4, left: 7, bottom: 4, right: 7)
        button.backgroundColor = UIColor(red: 236.0/255.0, green: 243.0/255.0, blue: 249.0/255.0, alpha: 1.0)
    return button
    }()

    let hashButton2: UIButton = {
        let button = UIButton()
        button.titleLabel?.font = UIFont(name: "Avenir-Medium", size: 13)
        button.setTitleColor(UIColor.black, for: .normal)
        button.titleEdgeInsets = UIEdgeInsets(top: 4, left: 7, bottom: 4, right: 7)
        button.backgroundColor = UIColor(red: 236.0/255.0, green: 243.0/255.0, blue: 249.0/255.0, alpha: 1.0)
        return button
    }()

}

class PostDetailText: BaseCell {

    override func setupViews() {
        super.setupViews()
        backgroundColor = UIColor.white

        textView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(textView)

        textView.frame = CGRect(x: 17, y: 25, width: contentView.frame.width - 34, height: 200)
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-17-[v0]-17-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : textView]))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : textView]))

    }

    let textView: UITextView = {
        let tv = UITextView()
        return tv
    }()

}

class BaseCell: UICollectionViewCell {

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

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupViews() {

    }

}

1 Ответ

0 голосов
/ 08 апреля 2019

Мое первое предположение, что .translatesAutoresizingMaskIntoConstraints = false не установлен для некоторых представлений.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...