Swift Transparent UINavigationBar - swift

I've to make my navigation bar transparent, I tried with this code :
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = UIColor.clear
But my navigation bar become white, I don't understand the reason!
Can someone help to solve this problem?

You are doing right way to make transparent UINavigationBar but seem like you have a wrong constraint. Make sure you top constraint of your scrollView is equal to Superview.Top.
If your top constraint of your scrollView is equal to Safe Area.Top, of course your ScrollView won't scroll below UINavigationBar.

Related

How to add a border to swift navigationBar after removing it

I use:
self.navigationController?.navigationBar.shadowImage = UIImage()
to remove the border of a navigationBar earlier in my app but now I'm not sure to add the border back on a later screen.
To reset the navigationBar's shadowImage to the default shadowImage do the following:
self.navigationController?.navigationBar.shadowImage = UINavigationBar().shadowImage

Reapply constraints after hiding UITabBar

I have a UIButton that is constrained to the view's safeAreaLayoutGuide bottom anchor and a UITabBar in that UIView. Everything is okay there. However, when I fullscreen an image, I hide the UITabBar. When I dismiss the fullscreen, I show the UITabBar again. However, the UIButton moves down and doesn't constrain to the UITabBar as it did before the UITabBar was hidden. The UIButton is covered partially by the UITabBar. Any solutions?
Here is the dismiss fullscreen code.
#objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
self.navigationController?.isNavigationBarHidden = false
self.tabBarController?.tabBar.isHidden = false
}
Here are two things you can try:
Solution 1:
Add this line to dismissFullscreenImage():
self.view.setNeedsLayout()
This invalidates the layout of self.view and causes it to be laid out again.
Solution 2:
You can avoid the need to relayout the view by making the navigationBar and tabBar invisible.
Instead of hiding/showing the navigationBar and tabBar by changing their isHidden properties, try setting their alpha values:
// hide
self.navigationController?.navigationBar.alpha = 0
self.tabBarController?.tabBar.alpha = 0
// show
self.navigationController?.navigationBar.alpha = 1
self.tabBarController?.tabBar.alpha = 1

How to create navigation bar translucent effect?

I have the table view with headers. Headers have only custom text and white background. Headers are pinned on top when scrolling and I want to know, is there any way how to make the background of headers translucent same as navigation bar is? With some blur effect?
Thanks for any advice!
Change the backgroundColor to .clear, add a UIVisualEffectView below the label and set the constraints to the edges. Not sure if it will mimic the behaviour of navbar by default so you would have to play around a little with the UIBlurEffect options to get the desired effect.
let blurEffectStyle = UIBlurEffectStyle.extraLight
let blurEffect = UIBlurEffect(style: blurEffectStyle)
let visualEffectView = UIVisualEffectView(effect: blurEffect)
Then add to view and set the constraints
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.view.backgroundColor = UIColor.clear
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.backgroundColor = UIColor.clear

Transparent NSCollectionView Background

I'm again struggling with setting NSViews background colors to transparent. I have a NSCollectionView as part of NSClipView which is part of a NSScrollview. My MainViewController has an outlet to the collectionView. With adding the two lines of code and after compiling the background is sometimes transparent but most of the times not:
view.wantsLayer = true
collectionView.layer?.backgroundColor = NSColor.clear.cgColor
I also tried to select/de-select the "Draw Background" property of the NSScrollView in the IB without any effects.
What do I miss here.
I struggled a bit attempting to get my NSCollectionView background "transparent" #JFS solution pointed me in the right direction: and I finally achieved it by setting both the parent scrollView and the collectionView backgrounds:
cvScrollView.backgroundColor = NSColor.clear
collectionView.backgroundColors = [NSColor.clear]
Ok, after a long evil trial and error phase I found a solution at least for myself. There are two background colors to set in order to have the proper behavior:
the background color of the NSScrollView:
the NSCollectionView primary color:
Both have to be set appropriately. At the point I set both to the same color I got the background I want. With setting only the ScrollView background color there was still the white background when scrolling the items in the CollectionView.
To make clear background for NSScrollView, the best option in Swift 4.2 is "Not draw a background". Let's get to view a programmatically example:
let scrollView = NSScrollView()
scrollView.drawsBackground = false
scrollView.contentView.drawsBackground = false
NSCollectionView has background for sections, so you need to specify colors for sections
let collectionView = NSCollectionView()
collectionView.backgroundColors = [.clear]
If you set backgroundColors to nil or to empty array, the background color is set by default to white.
If you set a background view for NSCollectionView, this array is ignored
You could try to put a NSView with frame zero as a backgroundView for NSCollectionView

Why I cannot make my UITabBarController blurred?

I have a UITabBar and I want to make it blurred. I wrote the following code:
import UIKit
class TabBarController:UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let blur = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurView = UIVisualEffectView(effect: blur)
blurView.frame = self.view.bounds
blurView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.view.layer.insertSublayer(blurView, atIndex: 0)
}
}
but somehow the last line throws error:
Cannot convert value of type 'UIVisualEffectView' to expected argument
type 'CALayer'
how can I fix that?
I changed the last line to:
self.tabBar.addSubview(blurView)
but now the whole tabbar is blurred (even with icons and they are not visible). When I changed this line to:
self.tabBar.sendSubviewToBack(blurView)
then the tabbar is visible, but not blurred. I want to achieve effect from accepted answer from here Black background on transparent UITabBar but here it is uitabbar and I'm using uitabbarcontroller... Can you help me with applying blur in my case?
You just add the blur view as a subview:
self.view.addSubview(blurView)
Since you just want to blue the tab bar and this class is a tab bar controller, you can do:
self.tabBar.addSubview(blueView)
You also need to change the frame:
blurView.frame = self.tabBar.bounds
why don't you just use the barTintColor property on your TabBarController?
self.tabBar.translucent = true
self.tabBar.barTintColor = UIColor.blackColor()
You don't even need to subclass UITabBarController. You can call this on any UIViewController.
self.tabBarController?.tabBar.translucent = true
self.tabBarController?.tabBar.barTintColor = UIColor.blackColor()
If I understood correctly from the following comment that you posted, you want to change the UITabBar to be black in colour but still blurred.
And yes, I noticed that the UITabBarController is blurred by default, but I would like to make it blurred with specific style (.Dark).
Doing this since iOS 7 has actually become quite easy. Simply change the barStyle of your UITabBar to .black. Put the following code in your UIViewController's viewDidLoad method (note that UITabBar is translucent by default, so you don't need to specify that again).
tabBarController?.tabBar.barStyle = .black
If you want to set it back to the regular, white barStyle, change it back to .default.
tabBarController?.tabBar.barStyle = .default
You may even do this from within Interface Builder by selecting the Tab Bar in your UITabBarController's hierarchy and changing its Style to Black.
I have a solution, all you need is configure your UITabBar as following:
// next code will make tabBar fully transparent
tabBar.isTranslucent = true
tabBar.backgroundImage = UIImage()
tabBar.shadowImage = UIImage() // add this if you want remove tabBar separator
tabBar.barTintColor = .clear
tabBar.backgroundColor = .black // here is your tabBar color
tabBar.layer.backgroundColor = UIColor.clear.cgColor
If you want to add blur, do this:
let blurEffect = UIBlurEffect(style: .dark) // here you can change blur style
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = tabBar.bounds
blurView.autoresizingMask = .flexibleWidth
tabBar.insertSubview(blurView, at: 0)
As a result:
Attach bottom constraint to the bottom of the view instead of Safe Area
It just might not be a problem with your TabBar but with tableView constraints.
Tab bar is blurred by default.