1

So I have a UITableView with a custom cell...

Here is the cell:

enter image description here

And here is the xib with a UITableView:

enter image description here

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:

enter image description here


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

  1. I deleted the parent UIView
  2. I created a new UIView
  3. I re-added the UITableView and 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.

Thanks for the help guys,

enter image description here

Community
  • 1
  • 1
Eamorr
  • 9,872
  • 34
  • 125
  • 209
  • Make Sure you have added layout margin code in Will display cell method.http://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working – Renish Dadhaniya Feb 28 '15 at 13:14
  • Have you already clicked ``Update Constraints`` in Storyboard/IB? Otherwise, iOS will use the auto-generated constraints at runtime – lukas Feb 28 '15 at 13:20
  • Yes, I tried this. I even reverted to the default UITableViewCell and I still see yellow margins... Totally stuck now... ;( – Eamorr Feb 28 '15 at 13:28
  • Please see updated question (update 2) – Eamorr Feb 28 '15 at 13:37

2 Answers2

2

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
 }
Teemu Kurppa
  • 4,779
  • 2
  • 32
  • 38
  • Thank you. Though it doesn't work - I still have the exact same - white margins on either side... – Eamorr Feb 28 '15 at 13:05
  • From the images it seems that there is margin between tableView and it's parent view, but I can't be sure. To debug, in your viewDidLoad, set `self.view.backgroundColor = UIColor.yellowColor()` and `self.tableView.backgroundColor = UIColor.cyanColor()` and in your cell creation code set `cell.contentView.backgroundColor = UIColor.redColor()`. – Teemu Kurppa Feb 28 '15 at 13:11
  • Hi Teemu, I just updated. Thank you for the suggestion. How do I get the tableview width to match the parent view? – Eamorr Feb 28 '15 at 13:13
  • Thank you for the suggestion. Unfortunately, when I add `self.view.layoutMargins = UIEdgeInsetsZero` there is no change - I can still see the yellow margins on either side. – Eamorr Feb 28 '15 at 13:21
  • @Eamorr can you Provide full width to your tableview? like tableview= self.view.bound.size.width. – Renish Dadhaniya Feb 28 '15 at 13:30
  • @RenishDadhaniya Thank you for your help. Please see update 2 in the original question... – Eamorr Feb 28 '15 at 13:37
  • @Eamorr can you provide demo source of it? – Renish Dadhaniya Feb 28 '15 at 13:40
1

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.

BSMP
  • 4,596
  • 8
  • 33
  • 44
Mauro
  • 11
  • 1