iOS 8 UITableView separator inset 0 not working

前端 未结 30 1820
清酒与你
清酒与你 2020-11-22 14:38

I have an app where the UITableView\'s separator inset is set to custom values - Right 0, Left 0. This works perfectly in iOS 7.

30条回答
  •  眼角桃花
    2020-11-22 15:00

    I believe this is the same question that I asked here: Remove SeparatorInset on iOS 8 UITableView for XCode 6 iPhone Simulator

    In iOS 8, there is one new property for all the objects inherit from UIView. So, the solution to set the SeparatorInset in iOS 7.x will not be able to remove the white space you see on the UITableView in iOS 8.

    The new property is called "layoutMargins".

    @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
    

    iOS 8 UITableView setSeparatorInset:UIEdgeInsetsZero setLayoutMargins:UIEdgeInsetsZero

    The solution:-

    -(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];
       }
    }
    

    If you set cell.layoutMargins = UIEdgeInsetsZero; without checking if the layoutMargins exists, the app will crash on iOS 7.x. So, the best way would be checking if the layoutMargins exists first before setLayoutMargins:UIEdgeInsetsZero.

提交回复
热议问题