XCode 6.3 adding margins to tableviewcell
Asked Answered
G

1

8

Did XCode 6.3 / Swift 1.2 add additional margins to a UITableViewCell's contentView? Prior to updating, I had a custom UIView that extended all the way across the screen in my cells. Example:

enter image description here

Now, everything in the cell seems to have additional margins that I have no idea where they came from.

enter image description here

Note that these view's widths are not altered in any way in code and the right and left are constrained as below:

enter image description here

Also note that I am using tableView.separatorStyle = .None. I add this fact because for some reason in one of my tableViews that has the default separator, it doesn't appear to add these additional margins.

Does anyone know if they did some weird change in XCode 6.3? This behavior occurred directly after updating.

Edit: enter image description here

Garble answered 18/4, 2015 at 2:19 Comment(4)
I was running this app on iOS 8 prior to updating and it looked like the first screenshot.Garble
i don't know what else I can add. I just tested it with constraints -16 on each side and it correctly expands across the screen, but I am unaware as to why my margins would be pushed over (I don't want to use a bandaid fix without knowing the cause like this). I printed the width of the contentView, view, tableView, and cell itself and they are all 375 on iphone 6 but the blue block is 359 width with the constraints given in the question.Garble
But why did you make the constraints to the margins in the first place? Make them to the actual edges of the content view, and then changes in the margin won't affect you. I realize that doesn't answer the root question, but assuming that the margins will be 8 and setting the constraints to -8 to compensate was pretty nutty to start with.Duax
Is this possible in interface builder? I just constrained it to contentview in the storyboard. I put an image in the main post to show.Garble
D
19

Look carefully at this screen shot of the Size inspector for a leading constraint:

enter image description here

See how "Relative to margin" is checked? That's your problem. If the margin changes, your left edge changes. Uncheck that menu item and then change the Constant to zero. Do that for the trailing constraint too, and your problems will be over.

Now let's address the deeper issue: what changed? You are absolutely right, something did. I believe they fixed a bug, and you got caught in the fix. Logging shows me that the cell's preservesSuperviewLayoutMargins is true and that the table's margins are 0,16,0,16. This is true even on iOS 8.2, so the effective margins on iOS 8.2 should have been 16. But they were 8, as if preservesSuperviewLayoutMargins were false. But in iOS 8.3, this setting is obeyed properly — with the result that you have observed.

Thus, another way to fix the problem would have been to leave your constraints as they are, but to set each cell's preservesSuperviewLayoutMargins to false in cellForRowAtIndexPath:. This works equally well to make the outcome identical in both systems.

EDIT Good news: it looks like this change is reverted in iOS 9. Thus, without change, your cells would look the same in iOS 9 as they did in iOS 8.2 and before.

Duax answered 18/4, 2015 at 3:5 Comment(5)
Edited to explain the underlying phenomenon: you were absolutely right, the margins did change! Thank you so much for pointing this out.Duax
..well that was some impressive debugging! Had to review all my table view cell's constraints. This fixed all of them. Thank you.Meniscus
You really got caught by Apple's new margin-based constraints. When you form a constraint initially using control-drag, it will be margin-based unless you hold the Option key to get the non-margin variant. Thus, margin-based constraints are an opt-out technology; you can opt out, but most people will not know to do so and will end up using them without even realizing it.Duax
Note that this has been acknowledged by Apple as a bug, and will be fixed, it seems, in iOS 9.Duax
@Duax Do you have any additional information about this Bug? I am running into this the other way round: I want to explicitly keep the margins to get a consistent look between custom and standard cells on all devices (and iOS 8.0 to 9.3). In some situations (iPhone6+ Landscape as pageSheet) the separatorInset differ from layoutMargins, too. The only viable solution I currently see is overriding all margins to a fixed value..Mixie

© 2022 - 2024 — McMap. All rights reserved.