iOS 8 UITableView 分隔符插入 0 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25770119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 8 UITableView separator inset 0 not working
提问by user3570727
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.
我有一个应用程序,其中UITableView
的分隔符插入设置为自定义值 - Right 0
, Left 0
。这在 中完美运行iOS 7.x
,但是在iOS 8.0
我看到分隔符插入设置为15
右侧的默认值。即使在 xib 文件中它设置为0
,它仍然显示不正确。
How do I remove the UITableViewCell
separator margins?
如何删除UITableViewCell
分隔符边距?
回答by cdstamper
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. However you will lose any system behavior like inheriting margins from the Safe Area so I do not recommend the below solution:
简单的解决方法是按照@user3570727 的建议对单元格进行子类化并覆盖布局边距属性。但是,您将丢失任何系统行为,例如从安全区域继承边距,因此我不推荐以下解决方案:
(ObjectiveC)
(目标C)
-(UIEdgeInsets)layoutMargins {
return UIEdgeInsetsZero // override any margins inc. safe area
}
(swift 4.2):
(快速 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 propertyto 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.
除了layoutMargins
属性之外,Apple 还向您的单元格添加了一个属性,以防止它继承您的表格视图的边距设置。设置此属性后,您的单元格可以独立于表格视图配置自己的边距。将其视为覆盖。
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.
此属性称为preservesSuperviewLayoutMargins
,将其设置为NO
将允许单元格的layoutMargin
设置覆盖layoutMargin
TableView 上设置的任何内容。它既节省时间(您不必修改表视图的设置),而且更加简洁。详细解释请参考 Mike Abdullah 的回答。
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.
注意:以下是单元级边距设置的干净实现,如 Mike Abdullah 的回答所示。设置单元格preservesSuperviewLayoutMargins=NO
将确保您的表格视图不会覆盖单元格设置。如果您确实希望整个表格视图具有一致的边距,请相应地调整您的代码。
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 shouldprevent your table view from overriding your cell margins. In some cases, it seems to not function properly.
将preservesSuperviewLayoutMargins
单元格上的属性设置为 NO可以防止表格视图覆盖单元格边距。在某些情况下,它似乎无法正常工作。
If all fails, you may brute-force your Table View margins:
如果一切都失败了,您可能会强制使用您的 Table View 边距:
-(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. Your mileage may vary, this is not documented very well.
编辑:穆罕默德·萨利赫带给我的注意的iOS 9可能发生变化,您可能需要设置表视图的cellLayoutMarginsFollowReadableWidth
到NO
,如果你想定制插图或利润。您的里程可能会有所不同,这没有很好地记录。
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:
编辑:这是一个纯粹的界面生成器方法:
NOTE: iOS 11 changes & simplifies much of this behavior, an update will be forthcoming...
注意:iOS 11 更改并简化了大部分行为,即将推出更新...
回答by user3570727
Arg!!! After playing around either doing this in your Cell
subclass:
啊!!!在您的Cell
子类中执行此操作后:
- (UIEdgeInsets)layoutMargins
{
return UIEdgeInsetsZero;
}
or setting the cell.layoutMargins = UIEdgeInsetsZero;
fixed it for me.
或cell.layoutMargins = UIEdgeInsetsZero;
为我设置固定它。
回答by Mike Abdullah
Let's take a moment to understand the problem before blindly charging in to attempt to fix it.
让我们花点时间了解问题,然后再盲目地尝试解决问题。
A quick poke around in the debugger will tell you that separator lines are subviews of UITableViewCell
. It seems that the cell itself takes a fair amount of responsibility for the layout of these lines.
在调试器中快速浏览一下会告诉您分隔线是UITableViewCell
. 似乎单元本身对这些线的布局承担了相当大的责任。
iOS 8 introduces the concept of layout margins. By default, a view's layout margins are 8pt
on all sides, and they're inheritedfrom ancestor views.
iOS 8 引入了布局边距的概念。默认情况下,视图的布局边距8pt
在所有边上,并且它们是从祖先视图继承的。
As best we can tell, when laying out out its separator line, UITableViewCell
chooses to respect the left-hand layout margin, using it to constrain the left inset.
尽我们所能,在布局分隔线时,UITableViewCell
选择尊重左侧布局边距,使用它来约束左侧插图。
Putting all that together, to achieve the desired inset of truly zero, we need to:
将所有这些放在一起,要实现真正零的理想插入,我们需要:
- Set the left layout margin to
0
- Stop any inherited margins overriding that
- 将左布局边距设置为
0
- 停止任何继承的边距覆盖
Put like that, it's a pretty simple task to achieve:
像这样,这是一个非常简单的任务:
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
Things to note:
注意事项:
- This code only needsto be run once per cell (you're just configuring the cell's properties after all), and there's nothing special about when you choose to execute it. Do what seems cleanest to you.
- Sadly neither property is available to configure in Interface Builder, but you can specify a user-defined runtime attribute for
preservesSuperviewLayoutMargins
if desired. - Clearly, if your app targets earlier OS releases too, you'll need to avoid executing the above code until running on iOS 8 and above.
- Rather than setting
preservesSuperviewLayoutMargins
, you canconfigure ancestor views (such as the table) to have0
left margin too, but this seems inherently more error-prone as you don't control that entire hierarchy. - It would probably be slightly cleaner to set only the left margin to
0
and leave the others be. - If you want to have a 0 inset on the "extra" separators that
UITableView
draws at the bottom of plain style tables, I'm guessing that will require specifying the same settings at the table level too (haven't tried this one!)
- 这段代码只需要在每个单元格中运行一次(毕竟您只是在配置单元格的属性),并且您选择何时执行它并没有什么特别之处。做你认为最干净的事情。
- 遗憾的是,这两个属性都无法在 Interface Builder 中进行配置,但您可以
preservesSuperviewLayoutMargins
根据需要为其指定用户定义的运行时属性。 - 显然,如果您的应用程序也面向较早的操作系统版本,则需要避免执行上述代码,直到在 iOS 8 及更高版本上运行。
preservesSuperviewLayoutMargins
您可以将祖先视图(例如表格)配置为具有0
左边距,而不是设置,但这似乎本质上更容易出错,因为您无法控制整个层次结构。- 仅将左边距设置为
0
并保留其他边距可能会稍微清洁一些。 - 如果您想
UITableView
在普通样式表底部绘制的“额外”分隔符上插入 0 ,我猜这也需要在表级别指定相同的设置(还没有尝试过这个!)
回答by Ricky
I believe this is the same question that I asked here: Remove SeparatorInset on iOS 8 UITableView for XCode 6 iPhone Simulator
我相信这与我在这里提出的问题相同:Remove SeparatorInset on iOS 8 UITableView for XCode 6 iPhone Simulator
In iOS 8, there is one new property for all the objects inherit from 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 8 中,所有从UIView
. 因此,SeparatorInset
在 iOS 7.x 中设置的解决方案将无法删除您在 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;
without checking if the layoutMargins
exists, the app will crash on iOS 7.x. So, the best way would be checking if the layoutMargins
exists first before setLayoutMargins:UIEdgeInsetsZero
.
如果在cell.layoutMargins = UIEdgeInsetsZero;
不检查是否layoutMargins
存在的情况下进行设置,应用程序将在 iOS 7.x 上崩溃。因此,最好的方法是检查layoutMargins
之前是否首先存在setLayoutMargins:UIEdgeInsetsZero
。
回答by Lukasz
You can use UIAppearance once, at your application startup (before UI is loaded), to set it as default global settings:
您可以在应用程序启动时(在加载 UI 之前)使用一次 UIAppearance,将其设置为默认全局设置:
// iOS 7:
[[UITableView appearance] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[[UITableView appearance] setSeparatorInset:UIEdgeInsetsZero];
[[UITableViewCell appearance] setSeparatorInset:UIEdgeInsetsZero];
// iOS 8:
if ([UITableView instancesRespondToSelector:@selector(setLayoutMargins:)]) {
[[UITableView appearance] setLayoutMargins:UIEdgeInsetsZero];
[[UITableViewCell appearance] setLayoutMargins:UIEdgeInsetsZero];
[[UITableViewCell appearance] setPreservesSuperviewLayoutMargins:NO];
}
This way, you keep your UIViewController's code clean and can always override it if you want.
这样,您可以保持 UIViewController 的代码干净,并且可以随时根据需要覆盖它。
回答by King-Wizard
iOS introduces the layoutMargins property on cells AND table views.
iOS 在单元格和表格视图上引入了 layoutMargins 属性。
This property isn't available in iOS 7.0 so you need to make sure you check before assigning it!
此属性在 iOS 7.0 中不可用,因此您需要确保在分配之前进行检查!
However, Apple has added a propertycalled preservesSuperviewLayoutMarginsto your cell that will prevent it from inheriting your Table View's margin settings. This way, your cells can configure their own margins independently of the table view. Think of it as an override.
但是,Apple 已向您的单元格添加了一个名为preservesSuperviewLayoutMargins的属性,以防止它继承您的 Table View 的边距设置。这样,您的单元格可以独立于表格视图配置自己的边距。将其视为覆盖。
This property is called preservesSuperviewLayoutMargins, and setting it to NO can allow you to override your Table View's layoutMargin settings with your own cell's layoutMargin setting. 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.
此属性称为preservesSuperviewLayoutMargins,将其设置为 NO 可以让您使用您自己的单元格的 layoutMargin 设置覆盖表视图的 layoutMargin 设置。它既节省时间(您不必修改表视图的设置),而且更加简洁。详细解释请参考 Mike Abdullah 的回答。
NOTE: this is the proper, less messy implementation, 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.
注意:正如 Mike Abdullah 的回答所表达的那样,这是正确的、不那么凌乱的实现;设置单元格的 preservesSuperviewLayoutMargins=NO 将确保您的表格视图不会覆盖单元格设置。
First step - Setup your cell margins:
第一步 - 设置您的单元格边距:
/*
Tells the delegate that the table view is about to draw a cell for a particular row.
*/
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
// Remove separator inset
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset = UIEdgeInsetsZero
}
// Prevent the cell from inheriting the Table View's margin settings
if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
cell.preservesSuperviewLayoutMargins = false
}
// Explictly set your cell's layout margins
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins = UIEdgeInsetsZero
}
}
Setting the preservesSuperviewLayoutMargins property on your cell to NO shouldprevent your table view from overriding your cell margins. In some cases, it seems not to function properly.
将您的单元格上的 preservesSuperviewLayoutMargins 属性设置为 NO应该可以防止您的表格视图覆盖您的单元格边距。在某些情况下,它似乎无法正常运行。
Second step - Only if all fails, you may brute-force your Table View margins:
第二步 - 只有当一切都失败时,你才可以蛮力你的 Table View 边距:
/*
Called to notify the view controller that its view has just laid out its subviews.
*/
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Force your tableview margins (this may be a bad idea)
if self.tableView.respondsToSelector("setSeparatorInset:") {
self.tableView.separatorInset = UIEdgeInsetsZero
}
if self.tableView.respondsToSelector("setLayoutMargins:") {
self.tableView.layoutMargins = UIEdgeInsetsZero
}
}
...and there you go! This should work on iOS 8 as well as iOS 7.
......你去吧!这应该适用于 iOS 8 和 iOS 7。
Note: tested using iOS 8.1 and 7.1, in my case I only needed to use the first step of this explanation.
注意:使用 iOS 8.1 和 7.1 进行测试,在我的情况下,我只需要使用本说明的第一步。
The Second Step is only required if you have unpopulated cell beneath the rendered cells, ie. if the table is larger than the number of rows in the table model. Not doing the second step would result in different separator offsets.
仅当渲染单元格下方有未填充的单元格时才需要第二步,即。如果表大于表模型中的行数。不执行第二步会导致不同的分隔符偏移。
回答by swilliams
In Swift it's slightly more annoying because layoutMargins
is a property, so you have to override the getter andsetter.
在 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
只读,在我的情况下很好。
回答by Julian Król
回答by Dan Beaulieu
Swift 2.0 Extension
Swift 2.0 扩展
I just wanted to share an extension I made to remove the margins from the tableview cell separators.
我只是想分享我为从 tableview 单元格分隔符中删除边距所做的扩展。
extension UITableViewCell {
func removeMargins() {
if self.respondsToSelector("setSeparatorInset:") {
self.separatorInset = UIEdgeInsetsZero
}
if self.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
self.preservesSuperviewLayoutMargins = false
}
if self.respondsToSelector("setLayoutMargins:") {
self.layoutMargins = UIEdgeInsetsZero
}
}
}
Used in context:
在上下文中使用:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
cell.removeMargins()
return cell
回答by Alexander Volkov
Swift:
迅速:
override func viewDidLoad() {
super.viewDidLoad()
if self.tableView.respondsToSelector("setSeparatorInset:") {
self.tableView.separatorInset = UIEdgeInsetsZero
}
if self.tableView.respondsToSelector("setLayoutMargins:") {
self.tableView.layoutMargins = UIEdgeInsetsZero
}
self.tableView.layoutIfNeeded() // <--- this do the magic
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset = UIEdgeInsetsZero
}
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins = UIEdgeInsetsZero
}
return cell
}