Please note, I'm going to refer to points as pixels in this question.
I have a grouped UITableView with 3 sections, each with a 40 pixel tall header view. The first header in the table view seems to be given a y position of 35 pixels by the system.
I've tried messing around with automaticallyAdjustsScrollViewInsets and a few other iOS 7 automatic pieces, but to no avail.
Why is my UITableView content being inset by 35 pixels?
EDIT: I've seen this answer and many other threads on this. I have valid headers and header heights. Also, setting the default to FLT_MIN, 0.01f, 1.0f or 100.0f doesn't fix the problem.
Here is my header implementation:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
UIView *header = (self.headerViews.count > section ? self.headerViews[section] : nil);
return (header.viewHeight ? : FLT_MIN);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *header = (self.headerViews.count > section ? self.headerViews[section] : nil);
return (header.viewHeight ? header : nil);
}
I'm also setting:
- (BOOL)automaticallyAdjustsScrollViewInsets{return NO;}
and
[self setSectionHeaderHeight:FLT_MIN];
It seems that this:
[self setTableHeaderView:[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, width, FLT_MIN)]];
In init, where self is a UITableView, will get rid of the top 35 points.
Yep,i get this problem when i set tableview.tableFooterView and custom set section header height, then first section height has over 35 points.
Then i add a
tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
worked for me.
There are multiple ways of getting the same issue you are getting and therefore, multiple solutions. I will try to list the ones I know with their corresponding solutions. Most of them are copied from this question.
UITableView inside a UIViewController
This is a common one that trips people off because they think the problem is related to their UITableView and most of the time is actually the parent UIViewController.
If your VC is embedded on a NavigationController. You will get a 35 points y offset as mentioned here.
Solutions
In Xcode Version > 5 on VC untick Extended Edges "Under Top Bars under the Attributes Inspector to remove the top UITableView content inset.
Constraints: Your VC has a main view where all the other subviews are laid including your UITableView. You need to make sure that all constraints from your UITableView are explicitily set and satisfied/non-ambiguous.
Set self.navigationController.navigationBar.translucent = YES;
Set self.automaticallyAdjustsScrollViewInsets = NO;. This one can also be set on the storyboard by unchecking the Adjust Scroll View Insets checkbox for the view controller layout.
Set self.edgesForExtendedLayout = UIRectEdgeNone;
General UITableView/UITableViewController
Set the tableView.separatorInset = UIEdgeInsetsZero;
Set the tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0); Pay attention to the -35. The negative number offsets the view.
Declare this method (Can also be used for FooterSection):
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
Related
I need to add a UIButton in the first section header of UITableView.
Here is my code :
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (section == 0) {
UIButton *createGroupBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, tableView.frame.size.width - 100, 50)];
// other code...
return createGroupBtn;
}
return nil;
}
You see, I have added constraints to the header's frame. (e.g the width is less 100 than tableview's frame's.)
The following is the result in my iPhone:
Obviously, the red button's width still equals the tableview's.
Do I miss some other important code to make the frame constraint work ?
First of all, you're not adding any constraints, you're just setting frames. A section header is always the same width as the table. If you want the button to be narrower, then you should create a UIView, add the button to it (with whatever size you want), and then return the view as the section header.
In my UIViewController I have dragged a UITableView which has 2 custom Prototype Cells. Basically Its a accordion.
When I am loading data on it there always a gap coming between my table view starting position and first row. when I scroll down Its working fine.I have searched in google and find couple of suggestion like
put
self.automaticallyAdjustsScrollViewInsets = YES;
In viewDidLoad OR add This below code in
UIEdgeInsets insets = UIEdgeInsetsMake(self.topLayoutGuide.length,
0.0,
self.bottomLayoutGuide.length,
0.0);
_detailsTableView.contentInset = insets;
or
YouStoryboard.storyboard > YouViewController > Attributes inspector > Uncheck - Adjust scroll view insets
But none of this worked for me. Here is my UITableView screenshot. Please suggest that how I can remove this gap marked in red.
After fixing this issue UITableView should looks like
Inspector Image 1
Try implementing this method:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
CGFloat height = UITableViewAutomaticDimension;
if (section == 0) {
height = 0.5f;
}
return height;
}
This will remove the spacing for the first section header. If you want to remove all spacings between sections you can always return 0.5f. Such an odd value is needed because Apple does not allow you to return 0 in this method. (That would have the same effect as not implementing the method at all.) But the value 0.5f will have the desired effect.
I think the space you are trying to get rid of is the header. Use the tableview delegate to set the header on section 0 to a height of 0.
tableView:heightForHeaderInSection
How to do set cell spacing in a section of UICollectionView? UICollectionView there is a property minimumInteritemSpacing, I need to set 1.0 still it not work. And I implemented the delegate method.
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 1.0;
}
To check about how your collection Cells would look, you can try this in your Storyboard with the help of storyboard. First just for the sake of checking, put some static cells in your CollectionViewController like this so that your screen appears like this :
No you can see those cells and the spacing between them. In your case, the cells will appear with improper spacing as you have shown above. So now open with this screen open, open up the Size Inspector fron your Interface Builder. It would look something like this :
Now you can see some options in the size inspector window. You can adjust the size of each cell and also the spacing between them using the Min. Spacing option. And finally for equal spacing from left and right sides, use the Section Insets Option. As you change the values there, the change will be reflected in your Controller View. So you can get an idea if you want to increase/decrease some values.
Hope this helps.
you can set programmatically like this
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
layout.minimumLineSpacing = 0.0;
layout.minimumInteritemSpacing = 0.0;
contestsCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
This method set up spaces between cells, footer and header:
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(20, 20, 20, 20);
}
Don't know why but I am having this issue when using with storyboard and autolayout.
Below is the screen shot of simulator...gray is the tableview background to understand the table frame, but my cell is not placed at the top side of table, causing some top space.
I don't want it.
It works well with ios6.
Place this code :
self.edgesForExtendedLayout = UIRectEdgeNone;
If not works than use this :
self.tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);
Set the TableView's header like this
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.01f)]
or set the tableview's header to nil like this
self.tableView.header = nil;
I don't know exactly what is causing this issue.
There may be a chance that you have set the height for header in section, but no view for header defined.
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return ;
}
Other than this I don't think any autolayout explicitly causing your cell to move down.
Cause your autolayout will either within your cell or table and its superview.
I have a UITableView in the grouped style, and only one section. However there is some blank space above and below the table view that is shown when the user scrolls too far. How can I remove this blank space?
You can do this by altering the contentInset property that the table view inherits from UIScrollView.
self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, -20, 0);
This will make the top and bottom touch the edge.
Add this code:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0;
}
Actually this question answered my question.
Reducing the space between sections of the UITableView.
UIView can be inserted at the top and bottom of the table(drag and drop). Set their properties as transparent and height of 1 px. This is to remove the extra padding in front of the cells.
you can also use this code for removing space between first cell of uitableview..
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.002f;// set this...
}
Uncheck Extend Edges Under Top bar.
This answer comes quite late, but I hope it helps someone.
The space is there because of the UITableView's tableHeaderView property. When the the tableHeaderView property is nil Apple defaults a view. So the way around this is to create an empty view with a height greater than 0. Setting this overrides the default view thereby removing the unwanted space.
This can be done in a Storyboard by dragging a view to the top of a tableView and then setting the height of the view to a value of 1 or greater.
Or it can be done programmatically with the following code:
Objective-C:
CGRect frame = CGRectZero;
frame.size.height = CGFLOAT_MIN;
[self.tableView setTableHeaderView:[[UIView alloc] initWithFrame:frame]];
Swift:
var frame = CGRect.zero
frame.size.height = .leastNormalMagnitude
tableView.tableHeaderView = UIView(frame: frame)
Comments
As others have noted you can use this same solution for footers.
Sources and Acknowledgements
See the Documentation for more details on the tableHeaderView property.
Thanks to #liushuaikobe for verifying using the least positive normal number works.
My original answer: https://stackoverflow.com/a/22185534/2789144
In my case issue was with the constraints i was applying. I have to change them in order to show 2 rows in my case while bottom of table touching last row.
Use the bounces property of UIScrollView:
[yourTableView setBounces:NO];
This will remove what seems to be an extra padding at the top and bottom of your UITableView.
Actually, it will just disable the tableview's scrollview to scroll past the edge of the content.