Wednesday, 8 October 2014

2 steps to use UITableViewCell separatorinset in ios 8 and setLayoutMargins in iOS 8

During porting from iOS 7.x objective-C based code in to iOS 8.x objective-C, I noticed that UITableviewCell separator disturbed from left and right side. Separator line got margin from left and right. It was fixed in iOS 7.x code using separator "inset" to 0. but it become default 15 in iOS 8 storyboard. even setting it  to zero (0) it didn't worked.

Actually in iOS 8 layoutMargins a new property is introduced. So problem comes in mind;  How to use layoutMargins on the cells and tableview in iOS 8?
it is missing in earlier version. so we need to put a check also to its availability.

Two fix it in iOS 8.x, need to use below 2 steps

1- For Cell

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

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

2- For Tableview

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) 
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) 
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];

Both steps need to be follow to achieve Separatorinset in iOS 8

This is the best way to use UITableViewCell separatorinset in ios 8 and  setLayoutMargins in iOS 8.

If you have some better way please share your valuable ideas in comment section.

1 comment:

  1. Thanks, Really nice post in easy steps. Saved my time.