iOS 8 UITableView 分隔符插入 0 不起作用 [英] iOS 8 UITableView separator inset 0 not working

查看:33
本文介绍了iOS 8 UITableView 分隔符插入 0 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中 UITableView 的分隔符插入设置为自定义值 - 右 0,左 0.这在 iOS 7.x 中完美运行,但是在 iOS 8.0 中我看到分隔符插入设置为右侧的默认值 15.即使在 xib 文件中它设置为 0,它仍然显示不正确.

如何删除 UITableViewCell 分隔符边距?

解决方案

iOS 8.0 在单元格和表格视图中引入了 layoutMargins 属性.

此属性在 iOS 7.0 上不可用,因此您需要确保在分配之前进行检查!

简单的解决方法是按照@user3570727 的建议对单元格进行子类化并覆盖布局边距属性.但是,您将丢失任何系统行为,例如从安全区域继承边距,因此我不推荐以下解决方案:

(目标 C)

-(UIEdgeInsets)layoutMargins {return UIEdgeInsetsZero//覆盖任何边距 inc.安全区}

(swift 4.2):

override var layoutMargins: UIEdgeInsets { get { return .zero } set { } }

如果您不想覆盖该属性,或者需要有条件地设置它,请继续阅读.

<小时>

除了 layoutMargins 属性之外,Apple 还向您的单元格添加了一个 属性,以防止它继承您的表格视图的边距设置.设置此属性后,您的单元格可以独立于表格视图配置自己的边距.将其视为覆盖.

此属性称为preservesSuperviewLayoutMargins,将其设置为NO 将允许单元格的layoutMargin 设置覆盖任何layoutMargin 设置在您的 TableView 上.它既节省时间(您不必修改表格视图的设置),而且更加简洁.详细解释请参考 Mike Abdullah 的回答.

注意:以下是单元级边距设置的干净实现,如 Mike Abdullah 的回答所示.设置单元格的 preservesSuperviewLayoutMargins=NO 将确保您的表格视图不会覆盖单元格设置.如果您确实希望整个表格视图具有一致的边距,请相应地调整您的代码.

设置单元格边距:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{//删除分隔符插入if ([cell RespondsToSelector:@selector(setSeparatorInset:)]) {[cell setSeparatorInset:UIEdgeInsetsZero];}//防止单元格继承表格视图的边距设置if ([cell RespondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {[cell setPreservesSuperviewLayoutMargins:NO];}//显式设置单元格的布局边距if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {[单元格 setLayoutMargins:UIEdgeInsetsZero];}}

Swift 4:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {//删除分隔符插入如果 cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {cell.separatorInset = .zero}//防止单元格继承表格视图的边距设置如果 cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {cell.preservesSuperviewLayoutMargins = false}//显式设置单元格的布局边距如果 cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {cell.layoutMargins = .zero}}

将单元格上的 preservesSuperviewLayoutMargins 属性设置为 NO 应该 防止表格视图覆盖单元格边距.在某些情况下,它似乎无法正常运行.

如果一切都失败了,您可能会强制使用您的 Table View 边距:

-(void)viewDidLayoutSubviews{[超级viewDidLayoutSubviews];//强制你的 tableview 边距(这可能是个坏主意)if ([self.tableView RespondsToSelector:@selector(setSeparatorInset:)]) {[self.tableView setSeparatorInset:UIEdgeInsetsZero];}if ([self.tableView RespondsToSelector:@selector(setLayoutMargins:)]) {[self.tableView setLayoutMargins:UIEdgeInsetsZero];}}

Swift 4:

func viewDidLayoutSubviews() {super.viewDidLayoutSubviews()//强制你的 tableview 边距(这可能是个坏主意)如果 tableView.responds(to: #selector(setter: UITableView.separatorInset)) {tableView.separatorInset = .zero}如果 tableView.responds(to: #selector(setter: UITableView.layoutMargins)) {tableView.layoutMargins = .zero}}

...然后你去!这应该适用于 iOS 7 和 8.

<小时>

Mohamed Saleh 提醒我注意 iOS 9 中可能发生的变化.您可能需要将表格视图的 cellLayoutMarginsFollowReadableWidth 设置为 NO 如果您想自定义插图或边距.您的里程可能会有所不同,这没有很好地记录.

该属性只存在于iOS 9,设置前请务必检查.

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

Swift 4:

if myTableView.responds(to: #selector(setter: self.cellLayoutMarginsFollowReadableWidth)) {myTableView.cellLayoutMarginsFollowReadableWidth = false}

(以上代码来自

注意:iOS 11 更改 &简化了大部分这种行为,即将进行更新......

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.x, however in iOS 8.0 I see that the separator inset is set to the default of 15 on the right. Even though in the xib files it set to 0, it still shows up incorrectly.

How do I remove the UITableViewCell separator margins?

解决方案

iOS 8.0 introduces the layoutMargins property on cells AND table views.

This property isn't available on iOS 7.0 so you need to make sure you check before assigning it!

The easy fix is to subclass your cell and override the layout margins property as suggested by @user3570727. However you will lose any system behavior like inheriting margins from the Safe Area so I do not recommend the below solution:

(ObjectiveC)

-(UIEdgeInsets)layoutMargins { 
     return UIEdgeInsetsZero // override any margins inc. safe area
}

(swift 4.2):

override var layoutMargins: UIEdgeInsets { get { return .zero } set { } }

If you don't want to override the property, or need to set it conditionally, keep reading.


In addition to the layoutMargins property, Apple has added a property to your cell that will prevent it from inheriting your Table View's margin settings. When this property is set, your cells are allowed to configure their own margins independently of the table view. Think of it as an override.

This property is called preservesSuperviewLayoutMargins, and setting it to NO will allow the cell's layoutMargin setting to override whatever layoutMargin is set on your TableView. It both saves time (you don't have to modify the Table View's settings), and is more concise. Please refer to Mike Abdullah's answer for a detailed explanation.

NOTE: what follows is a clean implementation for a cell-level margin setting, as expressed in Mike Abdullah's answer. Setting your cell's preservesSuperviewLayoutMargins=NO will ensure that your Table View does not override the cell settings. If you actually want your entire table view to have consistent margins, please adjust your code accordingly.

Setup your cell margins:

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

Swift 4:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    // Remove seperator inset
    if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        cell.separatorInset = .zero
    }
    // Prevent the cell from inheriting the Table View's margin settings
    if cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {
        cell.preservesSuperviewLayoutMargins = false
    }
    // Explictly set your cell's layout margins
    if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
        cell.layoutMargins = .zero
    }
}

Setting the preservesSuperviewLayoutMargins property on your cell to NO should prevent your table view from overriding your cell margins. In some cases, it seems to not function properly.

If all fails, you may brute-force your Table View margins:

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    // Force your tableview margins (this may be a bad idea)
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }

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

Swift 4:

func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    // Force your tableview margins (this may be a bad idea)
    if tableView.responds(to: #selector(setter: UITableView.separatorInset)) {
        tableView.separatorInset = .zero
    }
    if tableView.responds(to: #selector(setter: UITableView.layoutMargins)) {
        tableView.layoutMargins = .zero
    }
}

...and there you go! This should work on iOS 7 and 8.


EDIT: Mohamed Saleh brought to my attention a possible change in iOS 9. You may need to set the Table View's cellLayoutMarginsFollowReadableWidth to NO if you want to customize insets or margins. Your mileage may vary, this is not documented very well.

This property only exists in iOS 9 so be sure to check before setting.

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

Swift 4:

if myTableView.responds(to: #selector(setter: self.cellLayoutMarginsFollowReadableWidth)) {
    myTableView.cellLayoutMarginsFollowReadableWidth = false
}

(above code from iOS 8 UITableView separator inset 0 not working)

EDIT: Here's a pure Interface Builder approach:

NOTE: iOS 11 changes & simplifies much of this behavior, an update will be forthcoming...

这篇关于iOS 8 UITableView 分隔符插入 0 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆