How to adjust height of UICollectionView to be the height of the content size of the UICollectionView?

后端 未结 14 1007
死守一世寂寞
死守一世寂寞 2020-11-28 03:51

I would like the UICollectionView (The red one) to shrink to the height of the content size in this case UICollectionViewCells(the yellow ones) because there is a lot of emp

相关标签:
14条回答
  • 2020-11-28 04:25

    1) Set Fix Height of your CollectionView.

    2) Create Outlet of this CollectionView Height Constant. Like :

    IBOutlet NSLayoutConstraint *constHeight;

    3) Add below method in your .m file:

    - (void)viewDidLayoutSubviews {
        [super viewDidLayoutSubviews];
    
        CGFloat height = collectionMenu.collectionViewLayout.collectionViewContentSize.height;
        constHeight.constant = height;
    }
    
    0 讨论(0)
  • 2020-11-28 04:29

    first of all calculate number of cells than multiply it with height of cell and then return height in this method

        collectionView.frame = CGRectMake (x,y,w,collectionView.collectionViewLayout.collectionViewContentSize.height); //objective c
        //[collectionView reloadData];
       collectionView.frame = CGRect(x: 0, y: 0, width: width, height: collectionView.collectionViewLayout.collectionViewContentSize.height) // swift
    
    0 讨论(0)
  • 2020-11-28 04:29

    Get the height of the cell. Something like this

    let cellHeight = cell.frame.height
    

    Get the origin of the collection view

    let cvOrigin = collectionView.frame.origin
    

    Get the width of the collection view

    let cvWidth = collectionView.bounds.width
    

    Set the frame of the content view

    collection.frame = CGRect(x: cvOrigin.x, y: cvOrigin.y, width: cvWidth, height: cellHeight )
    
    0 讨论(0)
  • 2020-11-28 04:30

    If you set the height constraint of the collection view. Just observe the contentSize change in the viewDidLoad and update the constraint.

            self.contentSizeObservation = collectionView.observe(\.contentSize, options: [.initial, .new]) { [weak self] collectionView, change in
                guard let `self` = self else { return }
                guard self.collectionView.contentSize != .zero else { return }
                self.collectionViewHeightLayoutConstraint.constant = self.collectionView.contentSize.height
            }
    
    0 讨论(0)
  • 2020-11-28 04:32

    In Swift 5 and Xcode 10.2.1

    1. Fix Height of the CollectionView

    2. Create Outlet of your CollectionViewHeight

      IBOutlet weak var myCollectionViewHeight: NSLayoutConstraint!
      
    3. Use below code

      override func viewDidLayoutSubviews() {
          super.viewDidLayoutSubviews()
          let height = myCollectionView.collectionViewLayout.collectionViewContentSize.height
          myCollectionViewHeight.constant = height
          self.view.layoutIfNeeded()
      }
      
    0 讨论(0)
  • 2020-11-28 04:32

    Do following.

    1. first set height constrain for UICollectionView
    2. here calendarBaseViewHeight is UICollectionView height Variable
    3. call the function after reload the collection view

      func resizeCollectionViewSize(){  
             calendarBaseViewHeight.constant = collectionView.contentSize.height      
       }
      
    0 讨论(0)
提交回复
热议问题