1

I tried lots of answer from stack overflow. But I did not get relevant answer for iOS 11. As you can notice I got space at top in UICollectionview. Please help me to fix this issue.

ScreenShort :

Result

StoryBoard

What I tried :

1) self.automaticallyAdjustsScrollViewInsets = false

This one is deprecated in iOS 11

2) Uncheck "Adjust Scroll View insets"

3) This link

Tushar Lathiya
  • 674
  • 6
  • 20

4 Answers4

3

automaticallyAdjustsScrollViewInsets is deprecated in iOS 11. Try to set contentInsetAdjustmentBehavior for iOS 11.

if #available(iOS 11.0, *) {
        collectionView.contentInsetAdjustmentBehavior = .never
    } else {
        self.automaticallyAdjustsScrollViewInsets = false
    }
iVarun
  • 5,696
  • 2
  • 21
  • 31
1

Try adding edgesForExtendedLayout = [] in your viewDidLoad

as well as;

if #available(iOS 11.0, *) {
        collectionView.contentInsetAdjustmentBehavior = .never
    } else {
        self.automaticallyAdjustsScrollViewInsets = false
    }
David Lintin
  • 1,315
  • 12
  • 32
1

I had the same problem in Xcode 11. And here is my solution:

enter image description here

Tuan Nguyen
  • 1,875
  • 1
  • 9
  • 11
0

Okay so :

The spacing issue could be mostly in part because you have the collectionView inside the UIView. Which by the way you should only use 1 collection view and you only need 1 view controller.

1.

Just put the collection view inside the first VC and replace the UIView with the collectionView.

2.

Then just drag another prototype reusable cell into the collection view. You can have multiple custom cells in one collection view in storyboard! Make cell 1 your first custom collection view cell type and cell 2 your second. When the segment bar switches just use a function to change the data the collectionView will present when you reload the collectionView, you can change the type of cell and array the collection view will present in the UICollectionViewDatasource methods.

Something along these lines:

enum MediaTypes {
  case pictures
  case videos
 }

fileprivate var arrayToPresent: MediaTypes = .pictures

...

func segmentChanged(){
 switch self.segmentController.selectedIndex {
    case 1:
     self.arrayToPresent = .videos

    default:
     self.arrayToPresent = .pictures
  }

  //Logic to reload Collection View
}   

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
 switch self.arrayToPresent {
  case .pictures:
    let cell = collectionView.deq... as! PictureCollectionViewCell
     return cell

  case .videos:
    let cell = collectionView.deq... as! VideoCollectionViewCell
     return cell
  }
}
Community
  • 1
  • 1
Milez
  • 21
  • 2