Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UICollectionView自调整大小不适用于长文本_Swift_Uicollectionview - Fatal编程技术网

Swift UICollectionView自调整大小不适用于长文本

Swift UICollectionView自调整大小不适用于长文本,swift,uicollectionview,Swift,Uicollectionview,下面的代码可以工作,但当涉及到长文本时,它会根据视图帧宽度剪切一些文本显示,例如将其替换为抽象或使用数据规范化以避免冗余。 将其替换为摘要或使用数据n显示未显示的其他内容 class AyatWordByWordViewController : UIViewController { let celled = "celled" var texts : [String] = ["Don't", "repeat", "yourself", "is a princip

下面的代码可以工作,但当涉及到长文本时,它会根据视图帧宽度剪切一些文本显示,例如
将其替换为抽象或使用数据规范化以避免冗余。

将其替换为摘要或使用数据n
显示未显示的其他内容

class AyatWordByWordViewController : UIViewController {

        let celled = "celled"

        var texts : [String] = ["Don't", "repeat", "yourself", "is a principle", "of", "software development", "aimed", "at", "reducing", "repetition of software patterns", "replacing it with abstractions or using data normalization to avoid redundancy."]

        //word by work
        var collectionView : UICollectionView!

        override func viewDidLoad(){

           let alignedFlowLayout = UICollectionViewFlowLayout()
            alignedFlowLayout.scrollDirection = UICollectionView.ScrollDirection.vertical
            collectionView = UICollectionView(frame: .zero, collectionViewLayout: alignedFlowLayout)
            collectionView.delegate = self
            collectionView.dataSource = self
            collectionView.register(AyatTableViewWordByWordItemCell.self, forCellWithReuseIdentifier: celled)
            collectionView.backgroundColor = UIColor.clear
           self.view.addSubview(collectionView)
                collectionView.anchor(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 150, leftConstant: 10, bottomConstant: 0, rightConstant: 10, widthConstant: 0, heightConstant: 0)


            if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
               // flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
                flowLayout.estimatedItemSize = CGSize(width: 1, height:1)
            }
        }
    }

    extension AyatWordByWordViewController : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return texts.count
        }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier:celled, for: indexPath) as! AyatTableViewWordByWordItemCell
                cell.subTitleLabel.text = texts[indexPath.item]
                cell.backgroundColor = UIColor.blue

            return cell
        }

  }
单元格:

public func fillSuperview() {
    translatesAutoresizingMaskIntoConstraints = false
    if let superview = superview {
        leftAnchor.constraint(equalTo: superview.leftAnchor).isActive = true
        rightAnchor.constraint(equalTo: superview.rightAnchor).isActive = true
        topAnchor.constraint(equalTo: superview.topAnchor).isActive = true
        bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
    }
}
subTitleLabel.fillSuperview()
已使用:

public func fillSuperview() {
    translatesAutoresizingMaskIntoConstraints = false
    if let superview = superview {
        leftAnchor.constraint(equalTo: superview.leftAnchor).isActive = true
        rightAnchor.constraint(equalTo: superview.rightAnchor).isActive = true
        topAnchor.constraint(equalTo: superview.topAnchor).isActive = true
        bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
    }
}
subTitleLabel.fillSuperview()

请尝试下面的
code

 let layout = yourCollectionView?.collectionViewLayout as! UICollectionViewFlowLayout
 layout.itemSize = UICollectionViewFlowLayout.automaticSize        
 layout.estimatedItemSize = CGSize(width: 93, height: 31)
 layout.sectionInset = UIEdgeInsets(top: 0, left: 9, bottom: 0, right: 0)

用单元格给出标签的前导、尾随、顶部和底部约束,我希望它对您有用

请尝试下面的
code

 let layout = yourCollectionView?.collectionViewLayout as! UICollectionViewFlowLayout
 layout.itemSize = UICollectionViewFlowLayout.automaticSize        
 layout.estimatedItemSize = CGSize(width: 93, height: 31)
 layout.sectionInset = UIEdgeInsets(top: 0, left: 9, bottom: 0, right: 0)

给出标签的前导、尾随、,单元格的顶部和底部约束我希望它对您有效

您是否在标签上设置了0行?@iDevid我设置了吗?您可以发布单元格约束吗?@iDevid检查更新的代码您是否在标签上设置了
preferredMaxLayoutWidth
?您是否在标签上设置了0行?@iDevid我设置了吗?您可以发布单元格约束吗?@iDevid检查更新了吗code您是否在标签上设置了
preferredMaxLayoutWidth