Reputation: 10012
So I have a UITableView with a custom cell...
Here is the cell:
And here is the xib with a UITableView:
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:
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.
And here is the troublesome output with the unwanted margins:
Update for @Teemu Kurppa:
In viewDidLoad()
, I did:
self.view.backgroundColor = UIColor.yellowColor()
self.table.backgroundColor = UIColor.cyanColor()
In tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
, I did:
cell.contentView.backgroundColor = UIColor.redColor()
Update 2:
So I got it working
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.
Thanks for the help guys,
Upvotes: 1
Views: 6579
Reputation: 9
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.
Upvotes: 0
Reputation: 4839
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
}
Upvotes: 2