ios 如何在 UITableView 中设置分隔符的全宽
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26519248/
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
How to set the full width of separator in UITableView
提问by Timo Cengiz
I have a UITableView
where the separators don't have the full width. It ends like 10 pixels before the left side. I was playing around with this code in the viewDidLoad()
.
我有一个UITableView
分隔符没有全宽的地方。它在左侧前 10 个像素处结束。我在viewDidLoad()
.
self.tableView.layoutMargins = UIEdgeInsetsZero;
Also in the storyboard when you can select custom or default selectors. Now all the cells that are populated don't have the full-width selectors but the cells that are empty have full width.
同样在故事板中,您可以选择自定义或默认选择器。现在填充的所有单元格都没有全宽选择器,但空的单元格具有全宽。
How can I fix this?
我怎样才能解决这个问题?
采纳答案by Timo Cengiz
I got the answer from this post: iOS 8 UITableView separator inset 0 not working
我从这篇文章中得到了答案:iOS 8 UITableView separator inset 0 not working
Just add this code on your UITableViewController
只需将此代码添加到您的 UITableViewController
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
回答by MB_iOSDeveloper
This worked for me on iOS 8.4 - 9.0 devices using Xcode 6.4 and Swift 1.2:
这在使用 Xcode 6.4 和 Swift 1.2 的 iOS 8.4 - 9.0 设备上对我有用:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
return cell
}
Swift 5 Update:
斯威夫特 5 更新:
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
回答by HannahCarney
In your UITableViewCell
在你的 UITableViewCell
Go to Attributes Inspector in your Interface Builder and simply change "15" to 0. Do this for all the cells you wish to change.
转到界面生成器中的 Attributes Inspector 并将“15”更改为 0。对您希望更改的所有单元格执行此操作。
You may need to add [cell setLayoutMargins:UIEdgeInsetsZero];
to your tableViewCell
您可能需要添加[cell setLayoutMargins:UIEdgeInsetsZero];
到您的tableViewCell
回答by Wilson
for Swift 3 :
对于 Swift 3 :
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorInset = .zero
tableView.layoutMargins = .zero
}
回答by Sebastián Lara
- Select your
UITableViewCell
- Go to Atributes Inspector
- Go to Separatorand change it to "custom Insets"
- Set
left
and / orright
fields. (By defaultleft: 15
,right: 0
)
- 选择您的
UITableViewCell
- 转到属性检查器
- 转到分隔符并将其更改为“自定义插入”
- 设置
left
和/或right
字段。(默认情况下left: 15
,right: 0
)
Look how it works (using left: 100
):
看看它是如何工作的(使用left: 100
):
Result:
结果:
回答by H6.
I inherit from UITableViewController
and needed additional to the two insets settings in willDisplayCell
also to set preservesSuperviewLayoutMargins
to false. In Swift it looks like this:
我继承UITableViewController
并需要额外的两个插图设置,willDisplayCell
也设置preservesSuperviewLayoutMargins
为 false。在 Swift 中,它看起来像这样:
override func tableView(_tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset = UIEdgeInsetsZero
}
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins = UIEdgeInsetsZero
}
if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
cell.preservesSuperviewLayoutMargins = false
}
}
回答by Ramin
回答by Michael Rose
For people having issues with the iPad — this will get you in a state that is the same as the iPhone. You can then adjust the separatorInset
as needed.
对于 iPad 有问题的人——这会让你处于与 iPhone 相同的状态。然后,您可以separatorInset
根据需要进行调整。
tableView.cellLayoutMarginsFollowReadableWidth = false
回答by Christopher Larsen
For Swift in iOS 9+
适用于 iOS 9+ 中的 Swift
If using a custom UITableViewCell
:
如果使用自定义UITableViewCell
:
override var layoutMargins: UIEdgeInsets {
get { return UIEdgeInsetsZero }
set(newVal) {}
}
then for your UITableView
in viewDidLoad
:
那么对于你的UITableView
in viewDidLoad
:
self.tableView?.separatorInset = UIEdgeInsetsZero;
self.tableView?.layoutMargins = UIEdgeInsetsZero;
回答by Aks
Use it in cellForRowAtIndexPath
method, to configure cell's separator specs ,
it works perfect for iOS9.0+
在cellForRowAtIndexPath
方法中使用它,配置单元格的分隔符规格,
它适用于iOS9.0+
cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;