Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UITableView separator inset not working on iOS 8 with storyboards.

All the tableViews of my app are not respecting the property SeparatorInset- Custom - left = 0 on storyBoard. It was all working fine on iOS 7, but not anymore.

When I implement the two below methods:

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

-(void)viewDidLayoutSubviews
{
    // iOS 7 
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    // iOS 8 
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

It works correctly, I just don't understand why I can't keep setting this on storyboard which is much simpler.

Any thoughts?

like image 965
Jorge Avatar asked Sep 22 '14 13:09

Jorge


1 Answers

This code may help you. Include both layoutMargins and separatorInset, to support both iOS versions 7,8 For iOS8:

First configure your table view as follows:

if ([self.tableView respondsToSelector:@selector(layoutMargins)]) {
    self.tableView.layoutMargins = UIEdgeInsetsZero;
}

Then in your cellForRowAtIndexPath: method, configure the cell as follows:

if ([cell respondsToSelector:@selector(layoutMargins)]) {
    cell.layoutMargins = UIEdgeInsetsZero;
}

For version check you can use following things:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
  if ([self respondsToSelector:@selector(setLayoutMargins:)]) {
    cell.layoutMargins = UIEdgeInsetsZero;
  }
#endif
like image 126
Jay Mehta Avatar answered Oct 11 '22 01:10

Jay Mehta