CollectionView dynamic height with Swift 3 in iOS

后端 未结 4 1449
说谎
说谎 2020-12-18 06:16

Recently, I\'m trying to do some projects for practicing. So I watched the course \"Developing Apps for iOS\", Stanford University, CS193P, 2017.

And now, I\'m doing

4条回答
  •  眼角桃花
    2020-12-18 06:44

    Use the following code to change the height according to the text displayed:

     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(view.frame.width , 64)
    }
    override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 
    {
      let approximateWidthOfContent = view.frame.width - x
        // x is the width of the logo in the left 
    
      let size = CGSize(width: approximateWidthOfContent, height: 1000)
    
      //1000 is the large arbitrary values which should be taken in case of very high amount of content 
    
     let attributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 15)]            
     let estimatedFrame = NSString(string: user.bioText).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
     return CGSize(width: view.frame.width, height: estimatedFrame.height + 66)          
     }
    

提交回复
热议问题