本文翻译自:iOS 8 UITableView separator inset 0 not working

I have an app where the UITableView 's separator inset is set to custom values - Right 0 , Left 0 . 我有一个应用程序,其中UITableView的分隔符插入设置为自定义值 - 右0 ,左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. 这在iOS 7.x完美运行,但在iOS 8.0我看到分隔符插入在右侧设置为默认值15 Even though in the xib files it set to 0 , it still shows up incorrectly. 即使在xib文件中它设置为0 ,它仍然显示不正确。

How do I remove the UITableViewCell separator margins? 如何删除UITableViewCell分隔符边距?


#1楼

参考:https://stackoom.com/question/1k7z5/iOS-UITableView分隔符插入-不起作用


#2楼

Arg!!! 精氨酸! After playing around either doing this in your Cell subclass: 在你的Cell子类中执行此操作之后:

- (UIEdgeInsets)layoutMargins
{return UIEdgeInsetsZero;
}

or setting the cell.layoutMargins = UIEdgeInsetsZero; 或设置cell.layoutMargins = UIEdgeInsetsZero; fixed it for me. 为我修好了。


#3楼

In Swift it's slightly more annoying because layoutMargins is a property, so you have to override the getter and setter. 在Swift中,它稍微有些烦人,因为layoutMargins是一个属性,所以你必须覆盖getter setter。

override var layoutMargins: UIEdgeInsets {get { return UIEdgeInsetsZero }set(newVal) {}
}

This will effectively make layoutMargins readonly, which in my case is fine. 这将有效地使layoutMargins只读,这在我的情况下是好的。


#4楼

I believe this is the same question that I asked here: Remove SeparatorInset on iOS 8 UITableView for XCode 6 iPhone Simulator 我相信这是我在这里提出的问题: 删除iOS 8上的SeparatorInset UITableView for XCode 6 iPhone模拟器

In iOS 8 , there is one new property for all the objects inherit from UIView . iOS 8中 ,从UIView继承的所有对象都有一个新属性。 So, the solution to set the SeparatorInset in iOS 7.x will not be able to remove the white space you see on the UITableView in iOS 8. 因此,在iOS 7.x中设置SeparatorInset的解决方案将无法删除您在iOS 8中的UITableView上看到的空白区域。

The new property is called " layoutMargins ". 新属性称为“ layoutMargins ”。

@property(nonatomic) UIEdgeInsets layoutMargins
Description   The default spacing to use when laying out content in the view.
Availability  iOS (8.0 and later)
Declared In   UIView.h
Reference UIView Class Reference

The solution:- 解决方案:-

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(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];}
}

If you set cell.layoutMargins = UIEdgeInsetsZero; 如果你设置cell.layoutMargins = UIEdgeInsetsZero; without checking if the layoutMargins exists, the app will crash on iOS 7.x. 在不检查layoutMargins存在的情况下,应用程序将在iOS 7.x上崩溃。 So, the best way would be checking if the layoutMargins exists first before setLayoutMargins:UIEdgeInsetsZero . 因此,最好的方法是在setLayoutMargins:UIEdgeInsetsZero之前检查layoutMargins存在。


#5楼

iOS 8.0 introduces the layoutMargins property on cells AND table views. iOS 8.0在单元格和表视图上引入了layoutMargins属性。

This property isn't available on iOS 7.0 so you need to make sure you check before assigning it! 此属性在iOS 7.0上不可用,因此您需要确保在分配之前进行检查!

The easy fix is to subclass your cell and override the layout margins property as suggested by @user3570727. 简单的解决方法是子类化您的单元格并覆盖@ user3570727建议的布局边距属性。 However you will lose any system behavior like inheriting margins from the Safe Area so I do not recommend the below solution: 但是,您将丢失任何系统行为,如从安全区继承边距,因此我不建议使用以下解决方案:

(ObjectiveC) (的ObjectiveC)

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

(swift 4.2): (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. 除了layoutMargins属性之外,Apple还为您的单元格添加了一个属性,以防止它继承您的表格视图的边距设置。 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. 此属性称为preservesSuperviewLayoutMargins ,并将其设置为NO将允许单元格的layoutMargin设置覆盖在TableView上设置的任何layoutMargin 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. 有关详细说明,请参阅Mike Abdullah的答案。

NOTE: what follows is a clean implementation for a cell-level margin setting , as expressed in Mike Abdullah's answer. 注意:以下是针对单元级边距设置的简洁实现,如Mike Abdullah的回答所述。 Setting your cell's preservesSuperviewLayoutMargins=NO will ensure that your Table View does not override the cell settings. 设置单元格的preservesSuperviewLayoutMargins=NO将确保您的表视图不会覆盖单元格设置。 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 insetif ([cell respondsToSelector:@selector(setSeparatorInset:)]) {[cell setSeparatorInset:UIEdgeInsetsZero];}// Prevent the cell from inheriting the Table View's margin settingsif ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {[cell setPreservesSuperviewLayoutMargins:NO];}// Explictly set your cell's layout marginsif ([cell respondsToSelector:@selector(setLayoutMargins:)]) {[cell setLayoutMargins:UIEdgeInsetsZero];}
}

Swift 4: 斯威夫特4:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {// Remove seperator insetif cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {cell.separatorInset = .zero}// Prevent the cell from inheriting the Table View's margin settingsif cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {cell.preservesSuperviewLayoutMargins = false}// Explictly set your cell's layout marginsif 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. 将单元格上的preservesSuperviewLayoutMargins属性设置为NO 应该可以防止表视图覆盖单元格边距。 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: 斯威夫特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. 这应该适用于iOS 7和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. 编辑: Mohamed Saleh引起了我对iOS 9中可能发生的变化的注意。如果要自定义插入或边距,可能需要将Table View的cellLayoutMarginsFollowReadableWidthNO Your mileage may vary, this is not documented very well. 您的里程可能会有所不同,但未记录在案。

This property only exists in iOS 9 so be sure to check before setting. 此属性仅存在于iOS 9中,因此请务必在设置之前进行检查。

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

Swift 4: 斯威夫特4:

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

(above code from iOS 8 UITableView separator inset 0 not working ) (以上代码来自iOS 8 UITableView分隔符插入0不起作用 )

EDIT: Here's a pure Interface Builder approach: 编辑:这是一个纯粹的Interface Builder方法:

NOTE: iOS 11 changes & simplifies much of this behavior, an update will be forthcoming... 注意:iOS 11更改并简化了大部分此类行为,即将发布更新...


#6楼

As to what cdstamper suggested instead of the table view, adding below lines in the cell's layoutSubview method works for me. 至于cdstamper建议的是什么而不是表视图,在单元格的layoutSubview方法中添加以下行对我有用。

- (void)layoutSubviews
{[super layoutSubviews];if ([self respondsToSelector:@selector(setSeparatorInset:)])[self setSeparatorInset:UIEdgeInsetsZero];if ([self respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){[self setPreservesSuperviewLayoutMargins:NO];;}if ([self respondsToSelector:@selector(setLayoutMargins:)]) {[self setLayoutMargins:UIEdgeInsetsZero];}
}

iOS 8 UITableView分隔符插入0不起作用相关推荐

  1. iOS 15 UITableView Section间距变大

    解决方法: 单页面修改 if (@available(iOS 15.0, *)) { [self.tableView setSectionHeaderTopPadding:0.0f]; } 全局修改 ...

  2. android 5.0 ios 8,iOS 8与Android 5.0大比拼:功能相同 体验不同

    过去半年,Android和iOS的移动大战已经发生了一些有趣的变化.过去,两款移动操作系统的差别主要体现在功能和精致方面.传统上,Android以更多功能和更高的可定制性见长,iOS则更为精致. 但是 ...

  3. mysql insert 自增_MySQL自增列插入0值的解决方案

    在将数据库从MSSQL迁移到MySQL的过程中,基于业务逻辑的要求,需要在MySQL的自增列插入0值.在MSSQL中是这样完成的: string sql;sql = " set identi ...

  4. iOS.Animations.by.Tutorials.v2.0汉化

    翻译自:iOS.Animations.by.Tutorials.v2.0 demo:https://github.com/womendexinshijie/BahamaAirLoginScreenOC ...

  5. IOS 10 微信 ajax readystate=0 status=0 解决方法

    IOS 10 微信 ajax readystate=0 status=0 解决方法 参考文章: (1)IOS 10 微信 ajax readystate=0 status=0 解决方法 (2)http ...

  6. 淘客基地:拾牛IOS版更新至1.0.5版本

    拾牛IOS版更新至1.0.5版本 主要更新:修复商品详情页打不开问题.在APP store中,底部导航栏"更新"中,下拉进行刷新,点击更新即可.

  7. iOS.Animations.by.Tutorials.v2.0汉化(一)

    demo:https://github.com/womendexinshijie/BahamaAirLoginScreenOC 前五章将向你介绍动画API-UIKit框架.这个API是专门设计来帮助你 ...

  8. iOS开发之蓝牙4.0技术完美实现

      CSDN博客   BaiHuaXiu123  博客专家 iOS开发之蓝牙4.0技术完美实现 发表于2016/5/1 21:13:06  8034人阅读 前言 前端时间,同学在做项目过程中遇到 ...

  9. sql 语句中 Sum(*) Nvl(name,0) Coun(*)的作用

    SUM(name)D的作用就是计算当前列名字为name所有字段的和 NVL(name,0)的作用相当于判空语句,if为空则取后面的0 为值,若不为空取本身的值 COUNT(NAME)  对符合条件的数 ...

最新文章

  1. Python多线程(3)——Queue模块
  2. mvc中signalr实现一对一的聊天
  3. Redis 面试题 50 问,史上最全
  4. 记录Nginx模块开发
  5. Graphicsmagick linux 中文水印乱码-new
  6. iOS多线程之GCD小记
  7. 关于AdvancedDataGrid的header的数据传递
  8. Hadoop HA on Yarn——集群配置
  9. java请求百度短链接_长链接生成短链接Java源码(调用百度接口)
  10. 每日英语:Do Bicycle Helmet Laws Really Make Riders Safer?
  11. 排序算法-冒泡排序(入门级别)
  12. 20200113每日一句
  13. Microsoft Office Visio(Windows)无限弹窗的诡异问题
  14. javascript适合移动端的响应式瀑布流插件实例演示
  15. [现代控制理论]7_线性控制器设计_Linear Controller Design
  16. java计算时间差 (日时分秒)
  17. 关于ORA-12505, TNS:listener does not currently know of SID given in connect descript的一个解决思路
  18. php生成推广二维码海报、合成图片demo
  19. [MRCTF2020]天干地支+甲子
  20. 机器学习【吴恩达|周志华|李宏毅|算法】清单

热门文章

  1. android 设置activity启动退出动画 | 解决设置activity 动画不生效问题
  2. 【Java基础】字符串与数组
  3. 第十五周程序阅读-范型程序设计(3)
  4. Android10.0 日志系统分析(二)-logd、logcat架构分析及日志系统初始化-[Android取经之路]
  5. python 提取网关信息_python:使用netifaces模块获取本机IP网关等信息
  6. Spark读取Parquet格式的数据为Dataframe
  7. React-Native 使用真机和指定模拟器调试
  8. (0030) iOS 开发之跳转之转场动画
  9. php与ajax技术
  10. 递归删除N天前的文件夹及子文件夹下的特定文件