iOS 9 个 UITableView 分隔符插图(重要的左边距)

iOS 9 UITableView separators insets (significant left margin)

我在 iOS 9UITableViewUITableViewCell 之间的分隔符有问题。他们有显着的左边距。我已经有了删除 iOS 8 引入的间距的代码,但它不适用于 iOS 9。看起来他们添加了其他东西。我想它可能与 layoutMarginsGuide 有关,但我还没有弄清楚。有没有人遇到类似的问题并找到了解决方案?

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

}

好的,I have found out the solution。唯一需要做的就是在 UITableView 的呈现实例上设置标志 cellLayoutMarginsFollowReadableWidth

myTableView.cellLayoutMarginsFollowReadableWidth = NO;

我想在文档中找到一些参考,但看起来还没有准备好,only mentioned on diff page

由于该标志是在 iOS 9 中引入的以实现向后兼容性,您应该在尝试设置它之前添加一个检查:

if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
{
    myTableView.cellLayoutMarginsFollowReadableWidth = NO;
}

对于 Swift 2.0 你可以使用 #available 检查 iOS 版本。

if #available(iOS 9, *) {
    myTableView.cellLayoutMarginsFollowReadableWidth = false
}

另外需要用Xcode 7以上编译

编辑

请记住,如果您的分隔符看起来 "fine" 高达 iOS 8,那么这是唯一需要的修复,否则您需要进行更多更改。您可以在 SO 上找到如何执行此操作的信息。

这对我在 iOS 9.

中非常有效

对于OBJ-C

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(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];
        }
         return cell;
    }

完美解直到iOS 9

在viewDidLoad中

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

在 TableViewDelegate 方法中添加以下代码:

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

已接受的答案对我不起作用。直到我在 setLayoutMargins 之前移动 setCellLayoutMarginsFollowReadableWidth(iOS 8 仍然需要):

if([_tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) {
  _tableView.cellLayoutMarginsFollowReadableWidth = NO;
}

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

根据此处的不同答案,我可以使用 Swift:

中的这些代码行消除分隔符中的间隙
tableView.separatorInset = UIEdgeInsetsZero
tableView.layoutMargins = UIEdgeInsetsZero
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero

但我在正文之前仍然有这个小空白:

对于 iOS 8 和 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];
}

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

Swift2.2iOS9.3

在viewDidLoad中

tableView.cellLayoutMarginsFollowReadableWidth = false

在 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
    }
}

Swift 3.0 / 4.0

tableView.cellLayoutMarginsFollowReadableWidth = false

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        cell.separatorInset = UIEdgeInsets.zero
    }
    if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
        cell.preservesSuperviewLayoutMargins = false
    }
    if cell.responds(to: #selector(setter: UIView.layoutMargins)) {
        cell.layoutMargins = UIEdgeInsets.zero
    }
}

如果您想在界面生成器中执行此操作。默认的分隔符插入是 Automatic。通过选择下拉菜单将其更改为 custom

这是我针对 Swift 3.0/iOS 10 in XCode 8.2.1.

的解决方案

我已经为 UITableview 创建了一个子类,它适用于 IB 并以编程方式创建表格视图。

import UIKit

class EXCSuperTV: UITableView
{
    required init?(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
        setupView()
    }

    override init(frame: CGRect, style: UITableViewStyle)
    {
        super.init(frame: frame, style: style)
        setupView()
    }

    func setupView() {}
}

class EXCNoFooter: EXCSuperTV
{
    override func setupView()
    {
        super.setupView()
        //tableFooterView = UIView.Zero()
    }
}


class EXCMainTV: EXCNoFooter
{
    override func setupView()
    {
        super.setupView()
        separatorInset = UIEdgeInsets.zero
    }
}