iOS 9 UITableView分隔符插入(显着的左边距)

问题描述 投票:48回答:11

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

ios uitableview uikit xcode7 ios9
11个回答
69
投票

好的,I have found out the solution。唯一需要的是设置标志UITableViewcellLayoutMarginsFollowReadableWidth的呈现实例

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或更高版本编译它。

编辑

请记住,如果您的分隔符在iOS 8中看起来“很好”,这是唯一需要的修复,否则您需要更改一点。您可以在SO上找到有关如何执行此操作的信息。


1
投票

适用于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];
}

0
投票

这是我在XCode 8.2.1中对Swift 3.0 / iOS 10的解决方案。

我为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
    }
}

26
投票

如果要在界面构建器中执行此操作。默认分隔符插入是Automatic。选择下拉列表将其更改为custom

enter image description here


17
投票

Swift 2.2 iOS 9.3

在viewDidLoad中

tableView.cellLayoutMarginsFollowReadableWidth = false

在UITableView代理中

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

11
投票

适用于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];
    }
}

11
投票

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

5
投票

基于这里的不同答案,我能够在Swift中使用以下代码行消除与分隔符之间的差距:

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

但我仍然在文本之前有这个小差距:

enter image description here


4
投票

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

2
投票

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

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

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

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

}
© www.soinside.com 2019 - 2024. All rights reserved.