UICollectionView adds top margin

前端 未结 9 1635
走了就别回头了
走了就别回头了 2020-12-04 14:06

I want to put a UICollectionView control that shows thumbs horizontally (only a single line of thumbs). For some reason the UICollectionView push the thumbs 44 pixels down,

相关标签:
9条回答
  • 2020-12-04 14:23

    Maybe you can try to force this value at 0 using the Delegate flow layout of collection view :

    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
    {
        return UIEdgeInsetsMake(PADDING_TOP, PADDING_LEFT, PADDING_BOTTOM, PADDING_RIGHT);
    }
    

    Modify the value of your padding.

    0 讨论(0)
  • 2020-12-04 14:24

    As some others mentioned, viewController.automaticallyAdjustsScrollViewInsets has been deprecated since iOS 11. My solution...

    Swift 4.2, Xcode 10.1, iOS 12.1:

    For some reason, collectionView.contentSize.height was appearing smaller than the resolved height of my collection view. First, I was using an auto-layout constraint relative to 1/2 of the superview's height. To fix this, I changed the constraint to be relative to the "safe area" of the view.

    This allowed me to set the cell height to vertically fill my collection view using collectionView.contentSize.height:

    private func setCellSize() {
        let height: CGFloat = (collectionView.contentSize.height) / CGFloat(numberOfRows)
        let width: CGFloat = view.frame.width - CGFloat(horizontalCellMargin * 2)
    
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: width, height: height)
    }
    

    Before

    After

    0 讨论(0)
  • 2020-12-04 14:29

    Swift 3:

    First you want to set the viewControllers automaticallyAdjustsScrollViewInsets to false:

    self.automaticallyAdjustsScrollViewInsets = false
    

    Then, you should be able to adjust the edge insets accordingly:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        //top, left, bottom, right
        return UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
    }
    
    0 讨论(0)
  • 2020-12-04 14:34

    The issue may be in collection view's content insets. Try to add self.automaticallyAdjustsScrollViewInsets = NO; into view controller's viewDidLoad method.

    0 讨论(0)
  • 2020-12-04 14:36

    I found that adding:

    self.edgesForExtendedLayout = UIRectEdgeNone;
    

    In the view controller I was loading the UICollectionView in solved the problem as I couldn't get the accepted answer to work.

    The question I found this answer to can be found here and provides an extremely in-depth and interesting explanation of the difference between automatically adjusted scrolled view insets, extended layouts and edge for extended layouts.

    Well worth a read

    0 讨论(0)
  • 2020-12-04 14:39

    Try using self.feedCollectionView.contentInsetAdjustmentBehavior = .never on the collection view. This actually worked for me.

    0 讨论(0)
提交回复
热议问题