Swift UIView Opacity Programmatically - ios

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.

Related

Problem: Background color of programmatic UI remains black

I've created the UI of a modal that pops up when you press a Tab Bar Item within a Tab Bar Controller. This is the first time that I've accomplished this programmatically, so there may be something that I'm missing here, but I can't seem to change the background color of the modal at all. More specifically, I'm trying to make the background transparent, but it appears in black when the modal is presented, no matter what color I change it to. I'm not sure that it matters that I've programmatically added subviews to the main view (for example, a UIView called "titleContainer"), but want to note this here just in case.
Below is part of what I have in my code:
override func loadView() {
view = UIView()
view.backgroundColor = UIColor.clear
view.isOpaque = false
titleContainer = UIView(frame: CGRect(x: 0, y: 0, width: 414, height: 180))
titleContainer.layer.borderWidth = 1.0
titleContainer.layer.borderColor = UIColor.darkGray.cgColor
titleContainer.layer.backgroundColor = UIColor.black.cgColor
titleContainer.layer.cornerRadius = 12
titleContainer.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
titleContainer.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(titleContainer)
NSLayoutConstraint.activate([
titleContainer.bottomAnchor.constraint(equalTo: view.bottomAnchor),
titleContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor),
titleContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
titleContainer.heightAnchor.constraint(equalToConstant: 180),
])
}
UPDATE: In addition to the fact that the original code was adding a color to the layer of the UIView, the modal was also sub-classing UITabBarController and not UIViewController, hence why the background color of the modal seemed to be inheriting that of the first VC in the Tab Bar Controller. Changing the superclass to UIViewController ultimately resolved this issue.
You're adding a color to the layer of the UIView. Remove the following line to get the view's background color. Or you can update the layer's color instead.
titleContainer.layer.backgroundColor = UIColor.black.cgColor
To set the color of the previous ViewController's view background you could set it while transitioning as follows:
viewController.titleContainer.layer.backgroundColor = view.backgroundColor?.cgColor
So your solution is correct the subclassing of your modal controller. In the future, you may want to double check the parent class of your subclasses especially when something off is happening like in your case.
And secondly, another way to help you debug the application is to toggle the attributes of your controller, say. a background color, and if you change your first tab screen's bg color, and the modal copies the color, then you can get to a theory that your modal is somehow getting that attribute from the first tab screen.
Lastly, utilize the Xcode's debugging features. One of those is Debug View Hierarchy. - This will give you the hierarchies of your views in 3-dimensional perspective.
Make sure to select overCurrentContext as modalPresentation for the vc
vc.modalPresentation = overCurrentContext
// present the vc
vc.bColor = UIColor.red
and declare this inside the vc
var bColor:UIColor!
Then set it here
titleContainer.layer.backgroundColor = bColor
Make you super view color to clear for your presenter view controller
self.view.backgroundColor = .clear
Than write this where ever you want to present your view controller
let controllerObject = yourControllor object from the storyboard
controllerObject.modalPresentationStyle = .overFullScreen
controllerObject.modalTransitionStyle = .coverVertical
present(controllerOject, animated: true, completion: nil)

how to make visible other components in transparent UIVIEW in swift 5

I have used a UIVIEW . I want to make transparent the UIVIEW. But when I want to chage alpha 0.0 from storyboard for make UIVIEW transparent, textfield, label and other component also transparent. I want to make transparent UIVIEW, not other component of the view. Here is the image
Please help me to make visible other components in UIVIEW transparent
Set your UIView's backgroundColor to UIColor.clear.
Instead of changing the alpha of the UIView, you can make the background color of your UIView as white with alpha component 0.5 or something
myView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
If you want it to be completely transparent, you can set backgroundColor to clear from storyboard as well as code.
myView.backgroundColor = .clear
You just need to change the transparency not of the UIView itself, but of its
background color.
For example:
I set the color of the UIView transparent, while the UILabel remained its settings.
In order to create a semi transparent color, you can use the following code:
yourView.backgroundColor = UIColor.white.withAlphaComponent(0.5)
Hope this will help you!
Go to Main.Storyboard and select the view you want to make transparent.
From the Attribute Inspector select background color as custom.
Then set your desired color and decrease the percentage of Opacity of the color.

Is clipsToBounds property necessary?

I am doing an animation practice from Apple's tutorial. The task is do build a UI of a music player. Each of control buttons has a "shadow" view underneath it, which appears when button is pressed:
Here is my code in viewDidLoad():
background.layer.cornerRadius = background.bounds.width / 2
background.backgroundColor = .clear
The thing is that tutorial insists to also add a clipsToBounds property for each of shadow views:
background.layer.cornerRadius = background.bounds.width / 2
background.clipsToBounds = true
background.alpha = 0.0
When I run my code, everything works fine, and shadow views appear as circles. So why use clipsToBounds here, considering that view is clipping to bounds of itself? Am I missing something here?
And while it's in the code - is using .alpha property a better practice than changing backgroundColor to .clear and back for animation purposes?

Avoid status bar transparency while animating

I got the viewController with 2 elements:
view with labels and buttons
tableView
I'm getting the following animation by changing view's height constraint from 170 to 0 and than animating view.layoutIfNeeded() and tableView.layoutIfNeeded().
My goal is to hide menu when content offset of the tableView reaches some value.
This works fine, except I got an overlay of status bar over the moving content from my view. Are there any options to add a sublayer to status bar not to be transparent? Or any other suggestions?
Thanks!
Create a view, put it where the status bar will be, and set its background color to which ever color you require. For example:
let statusBarView = UIView(frame: UIApplication.sharedApplication().statusBarFrame)
statusBarView.backgroundColor = UIColor.blackColor()
view.addSubview(statusBarView)
Or, set the content edge inset by (20.0, 0.0, 0.0, 0.0), which I would agree is a much more elegant solution, as suggested by #holex in the comments

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