博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 8 UITableView分隔符插入0不起作用
阅读量:2291 次
发布时间:2019-05-09

本文共 9325 字,大约阅读时间需要 31 分钟。

本文翻译自:

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楼

参考:


#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: 我相信这是我在这里提出的问题:

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 layoutMarginsDescription   The default spacing to use when laying out content in the view.Availability  iOS (8.0 and later)Declared In   UIView.hReference UIView Class Reference

iOS 8 UITableView setSeparatorInset:UIEdgeInsetsZero setLayoutMargins:UIEdgeInsetsZero

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 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: 斯威夫特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. 将单元格上的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 ) (以上代码来自 )

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

转载地址:http://xjcnb.baihongyu.com/

你可能感兴趣的文章
设计模式
查看>>
ArcGIS 关于三维立体地图 简单使用,里面的资源就在 arcgis 的demo里面有
查看>>
杂物堆
查看>>
第三章(附录)----python-django下mysql的使用
查看>>
第三章-Django-pycharm的使用
查看>>
第四章--Django-sitting详解
查看>>
第五章-杂章
查看>>
第六章--admin
查看>>
第七章-models
查看>>
第一章 ----数据库设计
查看>>
第二章-xadmin管理
查看>>
第三章--用户登录
查看>>
第四章---用户注册、登陆
查看>>
chapter 8 课程页面
查看>>
chapter 9 课程讲师
查看>>
chapter 13 xadmin 优化
查看>>
chapter 3、项目配置
查看>>
chapter 5、Django restframework
查看>>
chapter 6
查看>>
开发流程
查看>>