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.)
Related
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.
I'm having quite a hard time messing with UIView's properties to achieve a desired opaque look programmatically that can be easily done through Storyboard and Attributes Inspector.
What I want to re-create (settings from attributes inspector):
A UIView with a background color of (RGB Sliders: 0, 0, 0) and Opacity Slider set at 75%, with alpha defaulted at 1. So basically a black UIView with the opacity toned down.
What I've tried programmatically:
1) view.backgroundColor = .black
view.alpha = 0.75
2) view.backgroundColor = UIColor.black.withAlphaComponent(0.75)
view.isOpaque = true
Attached is a picture of the UIView selected in Storyboard with the settings for you to see. If you need any more information, please don't hesitate to let me know. Much thanks for your help.
UPDATE: Thanks for all your input. The combination of Clever Error's explanation of view layers and Matt's code allowed for me to achieve my desired look.
Desired UI Look!
What you are describing is:
view.backgroundColor = UIColor.black.withAlphaComponent(0.75)
view.isOpaque = false
The view must not be marked opaque, as it is not opaque.
You can set opacity to view like below in Swift 3
view.alpha = CGFloat(0.1)
Summarizing these answers https://stackoverflow.com/a/40405424/7767664, https://stackoverflow.com/a/46669162/7767664
for UIView itself opacity
view.isOpaque = false
view.alpha = CGFloat(0.5)
for UIView Background opacity
view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
view.isOpaque = false
When your app runs, the view hierarchy will be
UIWindow
UINavigationControllers view
Your View Controllers View
The problem is that UIWindow is black by default, and the UINavigationControllers view is clear. So it ends up mixing your transparent black color with full black to get just black.
You would need to add another view to your view controller that is transparent black then set the view controllers view color to the one you want to blend with.
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.
Why is the background of my table view grey when there are no rows? I can't see where this is set in storyboard
Even setting the background to white explicitly I still get a grey background
Using the defaults for background also results in this grey background:
this might be due to two reason
a) you haven't set data source and delegate and your main view background color is gray
b) cell background color is gray, set that to clear color
Please add this code viewdidload or viewwillappear method
table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
For me it looks like it is a problem of the UITableViewCells, you should set them to
self.backgroundColor = [UIColor whiteColor]; // or clearColor
then also set the UITableView itself inside the controller to backgroundColor white or grey.
Also the snippet of #Rohitsuvagiya can help to let dissappear the separators if there is no additional UITableViewCell.
Please add line your code in viewdidload or viewillappear
table.backgroundColor=[UIColor clearColor];
Do you have any idea why alpha parameter does not work on my view ?
self.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.4)
please also check this video
Video
thanks
If you are using storyboard and present view controller using segue, the presentation attribute of segue that show you view controller with transparent background should be over full screen as shown in attachment.
Then in your view controller set the background color by using this :
self.view.backgroundColor = UIColor(white: 0, alpha: 0.4)
It's not strange, it's behaving exactly as it should. Although many of UIColor's methods are class methods, there are still a few instance methods, and this is one of them. From the UIColor documentation .
colorWithAlphaComponent:
Creates and returns a color object that has the same color space and
component values as the receiver, but has the specified alpha
component. So, colorWithAlphaComponent: just changes the alpha value
of its receiver. Example:
let black = UIColor.blackColor() // 1.0 alpha
self.view.backgroundColor = black.colorWithAlphaComponent(0.5) // 0.5 alpha
I found an issue on iOS 12 which is related to the title (but not the answers though) where withAlphaComponent does not work.
In a storyboard, set a view background colour to an opaque named colour
In the code (viewDidLoad of the viewController for instance), change the background colour of this view to the same named colour but with a different alpha (using withAlphaComponent)
Expected result: the view background colour is the the named one with some alpha.
Actual result: the view background colour is he named colour without alpha.
The issue does not occur if the colour set in the storyboard is any colour but the named one. It does not occur on iOS 14.1 either.
You are presenting the ViewController so it won't work for that you have to add UIView of UIViewController on the other ViewController after that it will work.
[[self view] addSubview:myViewController.view];