uicollectionview remove top padding
You can fix top padding issue by considering one of the following method.
Method 1: Natural way to fix your problem by setting up your collectionView dimensions
properly from StoryBoard
.
Method 2: **Updated**
You can validate collection frame
in viewDidLayoutSubviews
or viewWillLayoutSubviews
override func viewDidLayoutSubviews() {
collectionView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
}
Method 3: You can Adjust Scroll View Insets
from your StoryBoard
Attributes Inspector
.
Method 4: You can fix this issue programatically by adjusting CollectionView contentInset
.
collectionView.contentInset = UIEdgeInsets(top: **Any Value**, left: 0, bottom: 0, right: 0)
Output with top padding 5:
Output with top padding 44:
Output with top padding 64:
UICollectionView Top spacing
I haven't worked with UICollectionViewCompositionalLayout.list
, so this is just from some quick research and experimentation...
The default .headerMode
for UICollectionLayoutListConfiguration
is .none
... when using .insetGrouped
that results in the "extra space" we're seeing.
Curiously, there is also a headerTopPadding
property, which we might expect to be of help here. It's default is 0
... but that appears to only be applied if there IS a header view.
So, if we set .headerMode = .supplementary
and return an empty UICollectionReusableView
for the header, we can get rid of the extra space:
Now, the top space is the same as the default left/right padding.
Here's what I used for the header view:
class EmptyHeaderView : UICollectionReusableView {
static let reuseIdentifier:String = "emptyHeaderView"
}
and three edits to your configureCollectionView()
func:
func configureCollectionView() {
var layoutConfig = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
// 1
// we're going to supply an "empty" header supplementary view
layoutConfig.headerMode = .supplementary
let listLayout = UICollectionViewCompositionalLayout.list(using: layoutConfig)
myCollectionView.collectionViewLayout = listLayout
myCollectionView.isScrollEnabled = false
let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, CVData> { (cell, indexPath, item) in
var content = UIListContentConfiguration.cell()
content.text = item.name
content.textProperties.font.withSize(8.0)
content.textProperties.font = UIFont.preferredFont(forTextStyle: .body)
content.secondaryText = item.value
content.prefersSideBySideTextAndSecondaryText = true
content.textProperties.adjustsFontSizeToFitWidth = false
cell.contentConfiguration = content
}
dataSource = UICollectionViewDiffableDataSource<Section, CVData>(collectionView: myCollectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, identifier: CVData) -> UICollectionViewCell? in
// Dequeue reusable cell using cell registration (Reuse identifier no longer needed)
let cell = collectionView.dequeueConfiguredReusableCell(using: cellRegistration,
for: indexPath,
item: identifier)
// Configure cell appearance
cell.accessories = [.disclosureIndicator()]
return cell
}
// 2
// register a reusable supplementary view for the header
myCollectionView.register(EmptyHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: EmptyHeaderView.reuseIdentifier)
// 3
// provider for header suppleentary view
dataSource.supplementaryViewProvider = { (collectionView: UICollectionView, kind: String, indexPath: IndexPath) -> UICollectionReusableView? in
if kind == UICollectionView.elementKindSectionHeader {
if let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: EmptyHeaderView.reuseIdentifier, for: indexPath) as? EmptyHeaderView {
return headerView
}
}
fatalError("Something's wrong with the setup...")
}
}
UICollectionVIew: How can I remove the space on top first cell's row?
UICollectionView is descendant of UIScrollView class which has contentInset
property, setting -20 top inset fixes the problem
[self.calendarView setContentInset:UIEdgeInsetsMake(-20, 0, 0, 0)];
However, the problem comes from UIViewController's automaticallyAdjustsScrollViewInsets
property.
By documentation:
Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself.
That's why we get adjusted content insets for status bar. It's better to disable automatically adjusting than manually set value which doesn't match in result picture.
[self setAutomaticallyAdjustsScrollViewInsets:NO];
Remove inset spacing at bottom of UICollectionView
The final solution that worked for me (for anyone that runs into a similar situation):
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if section == 0 {
return UIEdgeInsetsMake(0, 0, 0, 1 * UIScreen.main.scale)
} else {
return UIEdgeInsets.zero
}
}
Grouped UICollectionView has extra 35 pixels of top padding
As you mentioned here
collectionView.contentInset = UIEdgeInsets(top: -35, left: 0, bottom: 0, right: 0)
I don't think it's a good solution case the real reason is produced by .grouped
. Controlling the whole contentView offset, contentInset
should not be used to offset the effect of header(show below).
When you specify the UICollectionLayoutListConfiguration with .grouped
mode,
Without any other code your listConfig
means listConfig.headerMode = .none
and listConfig.footerMode = .none
by default. The collectionView will produce a header and a footer for each section .
The 35 pixel comes from the height of your section header.In this case, I guess you only have one section, and as you are able to see, you must have the same extra padding at the bottom.
- Solution
1、listConfig.headerMode = .firstItemInSection
The convenient and simplest way
When you use this header mode, a UICollectionViewListCell object that appears as the first item in the section automatically uses a header appearance. When you configure your data source, make sure to account for the fact that the first item in the section (at index 0) represents the header, and the actual items in the section start at index 1.
2、listConfig.headerMode = .supplementary
You may totally custom your header within UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if (kind == UICollectionView.elementKindSectionHeader) {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "your headerIdentifier", for: indexPath)
... // do some custom things
return header
}
return UICollectionReusableView()
}
and don't forget this
collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "your headerIdentifier")
Related Topics
Instead of Push Segue How to Replace View Controller (Or Remove from Navigation Stack)
How to Open Mail App from Swift
Xcode 8 Build Crash on iOS 9.2 and Below
How to Keep Uitableview Contentoffset After Calling -Reloaddata
Force Landscape Mode in One Viewcontroller Using Swift
Swift - How to Detect Orientation Changes
Storing Images Locally on an iOS Device
How to Enable Back/Left Swipe Gesture in Uinavigationcontroller After Setting Leftbarbuttonitem
Identify New iPhone Model on Xcode (5, 5C, 5S)
How to Reload Tableview from Another View Controller in Swift
Getting Device Orientation in Swift
Set Dimensions for Uiimagepickercontroller "Move and Scale" Cropbox
Saving Image to Documents Directory and Retrieving for Email Attachment
Implement Pushkit and Test in Development Behavior
Conversion from Nstimeinterval to Hour,Minutes,Seconds,Milliseconds in Swift