Swift 4 backgroundColor bug? - ios

I have a problem with transparency in my Views, especially with UITableView.
Just to make sure. I already have 3 years of experience with swift and this is the first time, I am getting this problem.
So in apple's docs it says the following:
backgroundColor
The view's background color.
Changes to this property can be animated. The default value is nil, which results in a transparent background color."
But when applying clear color to backgroundColor or tableView.backgroundColor = UIColor.clear, the background is still appearing white.
Is this a bug or a feature?
EDIT:
The tableView isn't the problem anymore. It is the cell within the tableView.
Already tried solutions are clearing the background color of all subviews. Nothing changed.
EDIT 29.09.17
Ok, I debugged my application and got this:
UICachedDeviceWhiteColor and cachedColor.
A google searching also didn't help.
Edit 29.09.2017 Later that day ...
The problem occurs, when updating from swift 3 to swift 4.
I made a new project with swift 4, but it actually works fine.
Because it is a problem I have to solve. I will try some things and update this post from time to time.

In UITableViewCell and UICollectionViewCell there is a property view called contentView.
A common mistake is to manipulate the cell directly. Instead try to manipulate the cells content view:
Obj-c
UITableViewCell *cell = ....// get the cell
cell.backgroundColor = ... // BAD !
cell.contentView.backgroundColor = [UIColor redColor] // this is the correct way.
Swift
let cell = ...
cell.contentView.backgroundColor = UIColor.red // make sure its the contentView
also a couple small nuances with colors and opacity:
If you want only the background to have opacity (i.e. less than 100%) , make sure you are not changing the view's opacity (i.e. view.alpha = 0.5 BAD) - change the color's opacity value instead:
i.e.
cell.contentView.backgroundColor = [UIColor colorWithRed:... green:... blue:... alpha:0.5];
Another useful trick is to pause the application and press the "visual debug" button.
Search for your view and check it's attributes in the inspector

Found the problem:
When instantiating a variable which holds the view before displaying it, the background color of this view is white. The debugger calls this UICachedDeviceWhiteColor.
Why would you do that?
If you instantiate the view before it is displayed, you can switch with a menu between different views very fast with "bringSubview(toFront: ...).
How to fix:
To fix this you have to instantiate the view every time you want to show it.
Hope I could help some lost souls with this answer and Apple is going to fix this bug.

Verify that the view behind it is not white, then set the current view's (the one with the transparent background) modalPresentationStyle to overCurrentContext. This will allow views behind to show through the UIColor.clear background color.

If you are setting
tableView.backgroundColor = .red
and if it's an empty TableView then the Red will be visible. I give a headerView and FooterView also to visualise.
Now TableView Contents some data, so to reflect the color you need to set background color to cell.
class UserCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
override func layoutSubviews() {
super.layoutSubviews()
backgroundColor = .yellow
}
....
}
You can check out the original project here.

Related

Change UITableView background color

I know this question has been asked before but all the solutions I could find failed, I tried using "background = nil" method, I've tried override func UIColor and non of these methods work. When I run my app it has a red background but all the cells in the table view have white backgrounds. I am not using any prototype cells. I've also changed the background color in view and section index.
You can try use "cell.backgroundColor" in cellForRowAtIndexPath. ˆ_ˆ
Please try this :-
cell.backgroundColor = UIColor.clearColor()
in the UITableViewDataSource protocol implementation
cellForRowAtIndexPath

How can I make the background of UICollection view as transparent without making the cells in it transparent in Swift

I have a collection view and of course I also have the cells in it. If in the property inspector I change the alpha of collection view to 0, the cells in it also become transparent. Is there a way to make the background transparent of the collection view only so the image behind it is visible?
using in Swift 3:
collectionView.backgroundColor = UIColor.clear.withAlphaComponent(0)
You can change the cell colour to clear and set the background to nil to see whats underneath the collectionView. Like this:
collectionview.backgroundView = nil;
collectionview.backgroundColor = [UIColor clearColor];
A UICollectionViewCell's background is defaulted to clear. Simply set a background colour for your cells either in interface builder or in a subclass using:
self.backgroundColor = UIColor.WhiteColor()
if you drop collection view opacity to 0 the whole collection view will be invisible. In your case you want to show collection view cell only so collection view cell's background view should be UIColor.clear either collection view background. Programmatically like this
self.collectionView.backgroundView?.backgroundColor = UIColor.clear
self.collectionView.backgroundColor = UIColor.clear
In my case i need to show chat log like this so i used that code
my example case
In the UICollectionView set the background color to clearcolor in the attribute inspector.
Dropping UICollectionView's Opacity to 0 from attribute inspector will make it transperent.
P.S:
Bad practice, but will work.

UIRefreshControl tint color doesn't match given color

The refresh color doesn't match the tint color and looks different, i tryied to change tintAdjustmentMode but the result is the same
Just to note, the spinner and text color should be 0x2C76BE
tvc.refreshControl = [UIRefreshControl new];
tvc.refreshControl.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
tvc.refreshControl.tintColor = [UIColor colorWithHex:0x2C76BE];
tvc.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to query spectrum again" attributes:#{NSForegroundColorAttributeName:[UIColor colorWithHex:0x2C76BE]}];
I had a similar problem with UIRefreshControl not displaying the color correctly when the view loads and I call beginRefreshing(). If the user pulls to refresh, the control correctly displays the tintColor I've specified.
First, subclass the refresh control. Then, override the didMoveToWindow method in the subclass. The following code finds the element that's animated to create the spinner and sets its background color.
This code uses an extension to UIView to return all the view's subviews (I used Jon Willis' answer from Swift: Recursively cycle through all subviews to find a specific class and append to an array).
class CustomRefreshControl: UIRefreshControl {
override func didMoveToWindow() {
super.didMoveToWindow()
if let l = getNestedSubviews().first(where: { $0.layer is CAReplicatorLayer }), l.subviews.count > 0 {
l.subviews[0].backgroundColor = UIColor.orange //getNestedSubviews method is an extension of UIView as referenced above
}
}
The spinner has a CAReplicatorLayer whose view contains one subview. That subview is simply a rectangle implements the graphic element for the spinner. It's that graphic element you're coloring.
UIRefreshControl is a buggy class. I noticed that placing the tvc.refreshControl.tintColor = [UIColor colorWithHex:0x2C76BE]; inside an animation block (even of zero duration) would yield the expected the results. So I tested to do this hideous 'hack': dispatch_async(mainQueue, <#set tintColor#>); and that also give the right result. There might also be a dependency of the refreshcontrol on the the timing of calling -beginRefreshing or -endRefreshing too.
Because I was annoyed so much by UIRefreshControl's buggyness and limitation of only being usable in a UITableViewController, I created a fully customizable one of my own, usable with any type of UIScrollView (UICollectionView, UITableView). Note that I created this before UICollectionViewFlowLayout supported the sticky headers like a tableView, so my refreshcontrol does not work well when that option is on. Feel free to submit a fix ;).
You can find it here https://github.com/Joride/JRTRefreshControl (if this falls under the 'shameless plugging clause' I will remove this link, but I think it is relevant to the question.

Change the color of empty table view cells?

I changed the color of cells from the storyboard (did not use any code) but as you can see in this image:
empty cells stay white as default. I want to change those empty cells to red also but I am having trouble figuring out.
I also in my code setting cell.backgroundColor = [UIColor redColor]; with no success.
You just set tableView's backgroundColor:
yourTableView.backgroundColor = [UIColor redColor];
For others who like to use Storyboard, there's not a specific setting to set this, but in the Table View in Storyboard, you can go to the Identity Inspector, enter a "backgroundColor" and set it as the Color type in User Defined Runtime Attributes:
Not sure why this isn't a setting in storyboard, but this works!
In Swift 3 you can do this:
self.tableView.backgroundColor = UIColor.red
XCODE 8.2 AND SWIFT 3
For change the color for your tableview you can use two ways:
Use (TableViewController): You can call your TableView and change the color via code.
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.backgroundColor = UIColor.red
}
Use (StoryBoard): Select your TableView, and in the inspector tap Atributes then, in the down view find View Section and chosse the color that you want from the Background atribute.

CollectionView background clearColor not working

I'm developing a small collectionview 'framework' to behave like a browser tab bar (think chrome) on the iPad. The code is all done, custom flow layout, reordering, and so on and is organized as so :
• TabBarCollectionViewController .h/.m/.xib contains the high logic of the collection view (delegates + datasource methods). I have the xib to configure the collectionView settings and set the custom flow layout (I could do this programmatically, but oh well it's easier that way).
• CustomFlowLayout .h/.m (subclass of flow layout)
• TabBarCell .h/.m/.xib (subclass of collectionviewcell)
Then I'm adding the TabBarCVC as a childViewController on my main viewController (this viewController has many childViewController and subviews) and then as a subview.
At this point all is working fiiiiine.
Now the problem, it's so stupid i can't believe i haven't found a way to do this, the backgroundColor of the collectionView is not settable to clearColor. I can put it in gray or whatever color, but that it doesn't support transparency. The cell background color is also clear and does work.
I need the collectionView to be transparent to show the texture on the main view behind. Any insight would be much appreciated, or perhaps i'll fill my first radar to apple.
If i can't find any solution i'll just add the 'screenshot' of the texture supposed to be behind the collectionView and add it as a imageView in the collectionView's backgroundView.
In my case I had the background color in the storyboard set to Default. This caused it to have a black background. Changing it to Clear Color worked.
Try setting the color to clear and the background view to an empty view like so...
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
Ok so i'm feeling pretty stupid now. I left an empty UIView behind, acting as a container for the collectionView for a test. I simply forgot to remove it, all is working fine with a nice clearColor now...
Watch out when setting UICollectionViews Background Color in Storyboard:
The initially selected value Default is Black (counterintuitively).
You have to explicitly select Clear Color for the View to be transparent.
Also note that the Preview in Storyboard immediately changes when this is done 'right'...
The easiest solution is just pick anything color in color picker to change collectionview background then turn the opacity to 0%.
I solved it using in Swift 3:
collectionViewVideo.backgroundColor = UIColor.clear.withAlphaComponent(0)
Fogmeister's answer worked great. Adapted to Swift 3, it would be:
self.collectionView.backgroundColors = [NSColor.clear]
self.collectionView.backgroundView = NSView.init(frame: CGRect.zero)
Swift 4.0 from Fogmeister's answer
self.collectionView.backgroundColor = UIColor.clear
self.collectionView.backgroundView = UIView.init(frame: CGRect.zero)
To have a nice semi-transparent white background use:
collectionView.backgroundColor = UIColor(displayP3Red: 1.0, green: 1.0, blue: 1.0, alpha: 0.35)
In swift this work with me:
self.collectionView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.0)
What is did to fix it
From Storyboard set collection view background colour as clear colour
then set main view colour to any colour you want , (I set to white.)

Resources