xcode IOS Swift UITableView - 无法解释的边距 - 如何摆脱?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28781965/
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 Swift UITableView - unexplained margins - how to get rid of?
提问by Eamorr
So I have a UITableView with a custom cell...
所以我有一个带有自定义单元格的 UITableView ...
Here is the cell:
这是单元格:
And here is the xib with a UITableView:
这是带有 UITableView 的 xib:
I cannot explain the white margins on either side of the blue cells???
我无法解释蓝色单元格两侧的白色边缘???
I tried:
我试过:
override func viewDidLoad() {
super.viewDidLoad()
...
table.layoutMargins = UIEdgeInsetsZero
table.separatorInset = UIEdgeInsetsZero
}
I even tried the advice from iOS 8 UITableView separator inset 0 not working:
我什至尝试了来自iOS 8 UITableView separator inset 0的建议不起作用:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:Cell_Jobs = self.table.dequeueReusableCellWithIdentifier("Cell_Jobs") as Cell_Jobs
cell.area.text = "Hello"
// kill insets for iOS 8
let nf = NSNumberFormatter()
let version = nf.numberFromString(UIDevice.currentDevice().systemVersion)
if(version?.floatValue >= 8){
cell.preservesSuperviewLayoutMargins = false
cell.layoutMargins = UIEdgeInsetsZero
}
// iOS 7 and later
if(cell.respondsToSelector("setSeparatorInset:") ) {
cell.separatorInset = UIEdgeInsetsZero
}
return cell
}
But I still can't get the left and right margins to go away...
但我仍然无法让左右边距消失......
What am I doing wrong?
我究竟做错了什么?
XCode6.1.1. Building for iOS 8.
XCode6.1.1。为 iOS 8 构建。
And here is the troublesome output with the unwanted margins:
这是带有不需要的边距的麻烦输出:
Update for @Teemu Kurppa:
@Teemu Kurppa 的更新:
In viewDidLoad()
, I did:
在viewDidLoad()
,我做了:
self.view.backgroundColor = UIColor.yellowColor()
self.table.backgroundColor = UIColor.cyanColor()
In tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
, I did:
在tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
,我做了:
cell.contentView.backgroundColor = UIColor.redColor()
Update 2:
更新 2:
So I got it working
所以我让它工作了
- I deleted the parent UIView
- I created a new UIView
- I re-added the UITableView and UIActivityIndicatorView
- 我删除了父 UIView
- 我创建了一个新的 UIView
- 我重新添加了 UITableView 和 UIActivityIndicatorView
I copied and pasted (Apple+C) from an Objective-C xib (that was written over a year ago) into a new Swift xib - I suspect there was some funny settings lingering about that were causing these unexplained margins.
我将 (Apple+C) 从 Objective-C xib(写于一年多前)复制并粘贴到新的 Swift xib 中 - 我怀疑有一些有趣的设置导致这些无法解释的边距。
Thanks for the help guys,
谢谢各位大侠的帮助
回答by Teemu Kurppa
For cells, try this:
对于单元格,试试这个:
override func tableView(tableView: UITableView,
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
cell.separatorInset = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false
cell.layoutMargins = UIEdgeInsetsZero
}
But based on the edited information, it seems that you have margin in the container view of the table view. Either this comes from your constraints between table view and view (check them one by one) or you have layout margins in the view, in which case try;
但是根据编辑的信息,您似乎在表格视图的容器视图中有边距。这要么来自表格视图和视图之间的约束(一一检查它们),要么视图中有布局边距,在这种情况下尝试;
override func viewDidLoad() {
// ...
self.view.layoutMargins = UIEdgeInsetsZero
}
回答by Mauro
I had the same problem and the next line worked for me.
我遇到了同样的问题,下一行对我有用。
// (Using Swift 3.0)
cell.contentView.layoutMargins = UIEdgeInsets.zero
It's important to know that the content view inside the cell is the one that has the margin, and not the cell who has it.
重要的是要知道单元格内的内容视图是具有边距的视图,而不是具有边距的单元格。