Remove SeparatorInset on iOS 8 UITableView for Xcode 6 iPhone Simulator

I found a weird white space on UITableView for iPhone 6 Simulator (iOS 8) on Xcode 6 GM. I have tried to set the SeparatorInset from both storyboard and also the code, but the white space is till there.

The following code works on iOS 7 but not on iOS 8 (iPhone 6 simulator).

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
}

I attached screenshot below:

iPhone 6 Simulator weird white space on tableview

I am using AutoLayout by the way. I hope someone can show me a way to remove the weird white space on the TableView.


Solution 1:

Thanks Student for pointing me to the right direction with the comment "Try this self.myTableView.layoutMargins = UIEdgeInsetsZero;" This line of code will only work on iOS 8 because layoutMargins is only available from iOS 8. If I run the same code on iOS 7, it will crash.

@property(nonatomic) UIEdgeInsets layoutMargins
Description   The default spacing to use when laying out content in the view.
Availability  iOS (8.0 and later)
Declared In   UIView.h
Reference UIView Class Reference

enter image description here

Below is the right answer to solve this weird white space by setting the tableview layoutMargins and cell layoutMargins as UIEdgeInsetsZero if it exists (for iOS 8). And it will not crash on iOS 7 as well.

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

   if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
   }
}

See the screen shot below:-

enter image description here

Solution 2:

Try to create a UITableViewCell class category and add this getter

- (UIEdgeInsets)layoutMargins {
    return UIEdgeInsetsZero;
}

in iOS7 this will not be called cos there's no this property in SDK,and will not cause any crash; in iOS8 this will be called every time you use the cell

It works for me

Solution 3:

My solution with just three lines of code:

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)row{
    //
    // ... your code ...
    //
    if ([cell respondsToSelector:@selector(preservesSuperviewLayoutMargins)]){
        cell.layoutMargins = UIEdgeInsetsZero;
        cell.preservesSuperviewLayoutMargins = false;
    }
    return cell;
}

Solution 4:

IOS8 introduce a new concept named Configuring Content Margins , a new property named layoutMargins is also introduced , for the details of the property , please refer to the Apple Doc . The type of layoutMargins is UIEdgeInsets , by default the value is {8,8,8,8} . To remove the seperator line of TableView in IOS8 , in addition to set tableView.seperatorInset = UIEdgeInsetsZero , you must also do as :

First define the macro

#define isIOS8SystemVersion (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)

In the UITableViewDelegate method add :

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         static NSString *reuseId = @"cellReuseID" ;
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
         if(!cell){
             cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseId];     
         if(isIOS8SystemVersion){   
             cell.layoutMargins = UIEdgeInsetsZero;
             cell.preservesSuperviewLayoutMargins =NO ;
         }

    }

Doing these will remove the seperator line . You can also do as follow :

    UITableView *tableView = [[UITableView alloc] init];
    if(isIOS8SystemVersion){
         tableView.layoutMargins = UIEdgeInsetsZero ;
    }

and in the UITableViewDelegate method add :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *reuseId = @"cellReuseID" ;
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
     if(!cell){
         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseId];     
     if(isIOS8SystemVersion){   
         cell.layoutMargins = UIEdgeInsetsZero;
     }
}

Solution 5:

In my case in Xcode 6.2, in addition to Will Q's answer, I have to go to Main.storyboard > select the UITableViewCell > Attributes Inspector. Change Separator dropdown list from Default Insets to Custom Insets. Change the left inset from 15 to 0.

enter image description here