Search code examples
iosobjective-ciphoneuitableviewiphone-x

How to remove extra space from the top of the Table View in iPhone X using Objective C iOS


I have already developed an app in Objective C which is working fine in all the iPhone mobiles. But when I am running this app in iPhone X Simulator then I dont know how I am getting some extra space (about 20-22 pixels) in the top of the UITableView. I tried all this solutions, but none of the below helped me :

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

self.edgesForExtendedLayout = UIRectEdgeNone;

[tblProducts setSectionHeaderHeight:0];

self.automaticallyAdjustsScrollViewInsets = NO;

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

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

I know this can be possible by setting my table view contentInset like :

tblProducts.contentInset = UIEdgeInsetsMake(-30, 0, 0, 0);

Is there any other solution to resolve this issue for iPhone X?

I checked my table view frame in ViewDidLoad, It's (0, 64, WIDTH, HEIGHT), Is there any problem with Status bar?

Please suggest me. Thank you in Advance!


Solution

  • You can try setting contentInsetAdjustmentBehavior property to Never.

    In Swift, I have this in UICollectionView:

    if #available(iOS 11.0, *) {
        collectionView.contentInsetAdjustmentBehavior = .never
    }
    

    In Objective-C, the same applies to UITableView, set it like so:

    if (@available(iOS 11.0, *)) {
        [_tableView setContentInsetAdjustmentBehavior: UIScrollViewContentInsetAdjustmentNever];
    }