Why Is There Extra Padding At the Top of My Uitableview With Style Uitableviewstylegrouped in Ios7

Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7

I have found the cause of my original bug and created a sample project showcasing it. I believe there is an iOS7 bug.

As of iOS7, if you create a UITableView with the Grouped style, but do not have a delegate set on first layout, then you set a delegate and call reloadData, there will be a 35px space at the top that will never go away.

See this project I made showcasing the bug: https://github.com/esilverberg/TableViewDelayedDelegateBug

Specifically this file: https://github.com/esilverberg/TableViewDelayedDelegateBug/blob/master/TableViewDelayedDelegateBug/ViewController.m

If line 24 is active,

[self performSelector:@selector(updateDelegate) withObject:nil afterDelay:0.0];

there will be an extra 35 px space at the top. If line 27 is active and 24 is commented out,

self.tableView.delegate = self;

no space at the top. It's like the tableView is caching a result somewhere and not redrawing itself after the delegate is set and reloadData is called.

why extra space is at top of UITableView - simple

Yes, that other question is very much related. UITableViewStyleGrouped divides each section into a "group" by inserting that extra padding…similar to what it did pre-iOS7, but clear instead of colored by default, and just at the top instead of all the way around. If you don't want the padding by default, use UITableViewStylePlain.

Otherwise, if you need to keep the style the same, do what this other posted from that link recommended and change the content inset:

self.tableView.contentInset = UIEdgeInsetsMake(-36, 0, 0, 0);

Or do what this poster suggested and set the tableHeaderView's height to .-1, i.e. nearly 0:

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.01f)];

Extra padding above table view headers in iOS 15

Since iOS 15, UITableView contains a new property called sectionHeaderTopPadding which specifies the amount of padding above each section header.

tableView.sectionHeaderTopPadding = 0.0

Note: This applies only to the UITableView.Style.plain.

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")

UITableView edit mode appears on top of my views

I suspect that when creating your UITableViewCell subclass, you did not add your custom subviews to the cell's contentView.

From the documentation:

The content view of a UITableViewCell object is the default superview for content that the cell displays. If you want to customize cells by simply adding additional views, you should add them to the content view so they position appropriately as the cell transitions in to and out of editing mode.

Sounds like your problem exactly.

iOS 15: Remove empty space before cells in UITableView

Check if you are only seeing this issue on iOS 15. If so, this may be caused by the newly introduced UITableView.sectionHeaderTopPadding property. You will need to set this value to 0 in order to remove the spacing before section headings:

let tableView = UITableView()
tableView.sectionHeaderTopPadding = 0

// Etc.

This property is only available in iOS 15 so you will need an API check if building for earlier versions.

If you're not on iOS 15, this question has most of the answers to this issue.

Remove extra padding above section header?

Use sectionHeaderTopPadding. It targets this specific spacing.

The API is only available from iOS 15, but the extra spacing itself is present in iOS 15 runtimes only.

struct ContentView: View {

init() {
if #available(iOS 15.0, *) {
UITableView.appearance().sectionHeaderTopPadding = 0
}
}

...
}

Sample Image

Extra space at section header in UITableView with Autolayout

I just ran into this today and finally found an answer that makes sense and works:

self.tableView.sectionHeaderTopPadding = 0

Looks like this property was just added in iOS 15

https://stackoverflow.com/a/70196640/7798489



Related Topics



Leave a reply



Submit