iOS 9 UITableView separators insets (significant left margin)

后端 未结 11 2078
故里飘歌
故里飘歌 2020-12-02 16:41

I have a problem with separators between UITableViewCells in UITableView on iOS 9. They have the significant left margin. I already ha

相关标签:
11条回答
  • 2020-12-02 17:30

    If you want to do it in interface builder. The default separator inset is Automatic. Change it to custom by selecting the dropdown.

    0 讨论(0)
  • 2020-12-02 17:30

    Swift 2.2 iOS 9.3

    In viewDidLoad

    tableView.cellLayoutMarginsFollowReadableWidth = false
    

    In UITableViewDelegates

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if cell.respondsToSelector(Selector("setSeparatorInset:")){
            cell.separatorInset = UIEdgeInsetsZero
        }
        if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
            cell.preservesSuperviewLayoutMargins = false
        }
        if cell.respondsToSelector(Selector("setLayoutMargins:")){
            cell.layoutMargins = UIEdgeInsetsZero
        }
    }
    
    0 讨论(0)
  • 2020-12-02 17:30

    For iOS 8 and 9

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        if ([UITableView instancesRespondToSelector:@selector(setLayoutMargins:)]) [[UITableViewCell appearance] setLayoutMargins:UIEdgeInsetsZero];
        if ([UITableView instancesRespondToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) [[UITableViewCell appearance] setPreservesSuperviewLayoutMargins:NO];
    }
    

    and

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    
    0 讨论(0)
  • 2020-12-02 17:31

    Perfect Solution upto iOS 9

    In viewDidLoad

    - (void)viewDidLoad {
        [super viewDidLoad];
        //Required for iOS 9
        if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0) {
            self.testTableView.cellLayoutMarginsFollowReadableWidth = NO;
        }
    }
    

    In TableViewDelegate methods add following code:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
        // Remove seperator inset
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
    
        // Prevent the cell from inheriting the Table View's margin settings
        if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
            [cell setPreservesSuperviewLayoutMargins:NO];
        }
    
        // Explictly set your cell's layout margins
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    }
    
    0 讨论(0)
  • 2020-12-02 17:32

    Based on different answers here, I am able to remove the gap from separator with these lines of codes in Swift:

    tableView.separatorInset = UIEdgeInsetsZero
    tableView.layoutMargins = UIEdgeInsetsZero
    cell.separatorInset = UIEdgeInsetsZero
    cell.layoutMargins = UIEdgeInsetsZero
    

    But still I am having this small gap before the text:

    0 讨论(0)
提交回复
热议问题