CMash
CMash

Reputation: 2168

UIView ignoring background colour in XCode 8 (or layout issue)

I've been developing an app in XCode 7 and all was well. I thought "hey, let's update to iOS 10" so opened up the project in XCode 8 and started fixing warnings and looking for issues.

Now it seems that UIViews load up with blank backgrounds, either from storyboard or created in code. In storyboard I have a UIView with a redColor background but the first time it's displayed (in a UICollectionViewCell) it appears with a clearColor background, or so it would appear, debugging it everything looks fine under the hood. When I swipe away from the cell and come back to that same cell (either by swiping back or the cell being recycled) it gets its correct colour... I've tried [cell setNeedsDisplay] and [cell setNeedsLayout] to no avail...

Even if I try and set the backgroundColor in my code it doesn't help how it looks when initially displayed.

Nothing unexpected is setting the background colour (checked by making a custom class that overrides the function) and the view isn't hidden, nor has 0 alpha (subviews show fine).

I should point that is seems to be clearColor because some subview UILabels I've got are still showing up ok.

I don't think there's any more information I can provide but feel free to ask!

EDIT: Turns out the backgroundColor is probably being honoured fine but something odd is going on with the layout of the view. The frame seems fine but if I use clipsToBounds then my subview labels disappear so the views frame must be off-screen somewhere or zero-sized...

Upvotes: 0

Views: 1787

Answers (2)

biomiker
biomiker

Reputation: 3306

I had this problem and it does seem related to layout. In my case I just had to "Clear all Constraints" from the affected view and then the background color was rendered properly.

Upvotes: 0

CMash
CMash

Reputation: 2168

Seems like a bug in XCode 8 storyboards. Once Mike had helped me whittle it down to being some kind of layout issue I started searching on that instead of a backgroundColor issue and came across this useful thread: https://stackoverflow.com/a/39589860/1751266

If you revert the storyboard back to XCode 7 format then everything works again. Unfortunately you have to do that every time you make a change to the storyboard. But hey, it works...

Upvotes: 2

Related Questions