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

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

问题描述

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



如何删除 UITableViewCell 分隔符边距?

解决方案

iOS 8.0引入了layoutMargins单元格和表格视图上的属性。



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



此外,Apple已向您的单元格添加属性,这将阻止其继承您的表格视图的边距设置。设置此属性后,您的单元格可以独立于表视图配置自己的边距。将其视为覆盖。



此属性名为 preservesSuperviewLayoutMargins ,并将其设置为 NO 将允许单元格的 layoutMargin 设置覆盖在TableView上设置的 layoutMargin 。它既节省了时间(您不必修改表格视图的设置),而且更简洁。有关详细解释,请参阅Mike Abdullah的答案。



注意:以下是单元级别边距设置的干净实现,正如Mike Abdullah的回答所表达的那样。设置单元格的preserveSuperviewLayoutMargins = 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 :)]){
[cell setLayoutMargins:UIEdgeInsetsZero] ;
}
}

Swift 4:

  func tableView(_ tableView:UITableView,willDisplay cell:UITableViewCell,forRowAt indexPath:IndexPath){
//删除分隔符inset
if cell.responds(to:#selector(setter:UITableViewCell.separatorInset)){
cell.separatorInset = .zero
}
//阻止单元格继承Table View的边距设置
if cell.responds(to:#selector(setter:UITableViewCell.preservesSuperviewLayoutMargins)){
cell.preservesSuperviewLayoutMargins = false
}
//显式设置你的单元格layout margin
if cell.responds(to:#selector(setter:UITableViewCell.layoutMargins)){
cell.layoutMargins = .zero
}
}

将单元格上的preservesSuperviewLayoutMargins属性设置为NO 阻止您的表格视图覆盖哟你的细胞边缘。在某些情况下,它似乎无法正常运行。



如果全部失败,您可以强制执行您的表格视图边距:

   - (void)viewDidLayoutSubviews 
{
[super 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边距(这可能是一个坏主意)
if tableView.responds(to:#selector(setter:UITableView.separatorInset)){
tableView.separatorInset = .zero
}
if tableView.responds (to:#selector(setter:UITableView.layoutMargins)){
tableView.layoutMargins = .zero
}
}

......你去了!这适用于iOS 7和8。



编辑: Mohamed Saleh引起了我对iOS 9可能发生的变化的关注。如果要自定义插入或边距,可能需要将表视图的 cellLayoutMarginsFollowReadableWidth 设置为。您的里程可能会有所不同,但未记录在案。



此属性仅存在于iOS 9中,因此请务必在设置之前进行检查。

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

Swift 4:

 如果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!

Additionally, 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天全站免登陆