Lee Dong Kyoo
Lee Dong Kyoo

Reputation: 61

ios first cell is not start from top of UITableView

My Application has a navigation and UITableView. I set backgroundColor to my tableView. Look at the picture bellow. I don't understand why the first cell is not started from top of UITableView. When I use View Debbuger in Xcode, TableViewWrapperView is not fulled in UITableView.

I already searched many times. I removed check Adjust Scroll View Insets and set .zero tableview's contentinsets. At last I made New Project and make it again but I can't.

How can I resolve this problem!!

enter image description here

Upvotes: 6

Views: 8610

Answers (10)

Elliot Wang
Elliot Wang

Reputation: 31

I have try the code below, but it did not work.

self.edgesForExtendedLayout = UIRectEdgeNone;  
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;  
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

My solution:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0,
                                                              0.0,
                                                              self.tableView.frame.size.width,
                                                              1.0)];
    header.backgroundColor = [UIColor whiteColor];
    
    return header;
}

Upvotes: 0

Reeder32
Reeder32

Reputation: 89

solution post iOS 11

tableView.contentInsetAdjustmentBehavior = .never

Upvotes: 0

Nitin
Nitin

Reputation: 71

Found it for iOS 11 and above, use:

    self.tableView.contentInsetAdjustmentBehavior = .never

Upvotes: 7

Dhilip
Dhilip

Reputation: 498

This is for the souls like me who tried everything above and might have missed this. I accidentally used tableview style as Grouped instead of Plain.

So set your Tableview style to "Plain" instead of "Grouped" in xib/storyboard or in code as

Objective C

self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];

Swift 4,0

self.tableView = UITableView(frame: CGRect.zero, style: .plain)

Upvotes: 7

Vivek
Vivek

Reputation: 5223

Simple Solution for this issue

No need to code for this issue. Just follow below simple step

YourStoryboard.storyboard > YourViewController > Attributes inspector > Uncheck - Adjust scroll view insets.

Here I attached screenshot for reference.

enter image description here

If you like go with code I have also solution :)

- (void)viewDidLoad {
      self.automaticallyAdjustsScrollViewInsets = false
}

Upvotes: 12

Bucket
Bucket

Reputation: 449

Write this two line on viewdidload of your viewcontroller, it will fix your issue

self.edgesForExtendedLayout = UIRectEdgeNone;
self.automaticallyAdjustsScrollViewInsets = NO;

Upvotes: 0

Windindi
Windindi

Reputation: 412

The table view tries to adjust itself to accommodate the navigation bar assuming that navigation bar is translucent and it has to display itself behind it.

To resolve this issue in the storyboard/xib file set the view controller property extends edges under top bar to false as shown below.

enter image description here

Upvotes: 5

Lee Dong Kyoo
Lee Dong Kyoo

Reputation: 61

I answer my own question.

I don't know what is different to top layout guide and view. But I resolve to set top constraint 0 to view.

Thank you.

enter image description here

Upvotes: 0

Rikh
Rikh

Reputation: 4222

Open the storyboard, click on the UITableView. In your sizeInspector set the Headerand Footer in section height field to be 1.

Upvotes: 2

Jacob King
Jacob King

Reputation: 6157

Open your storyboard and click on the table view controller/view controller that contains the table view. Open the attribute inspector and untick Adjust Scroll View Insets.

Upvotes: 0

Related Questions