Trying to set a transparent navbar but it shows up black - swift

I have been trying to get a transparent navigationBar but it becomes black.
Here is the code i used to make it transparent:
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
Any help is much appreciated.

You can try out this UINavigationBar extension
Use
navigationController?.navigationBar.makeTransparent()
Extension
public extension UINavigationBar {
/// - Parameter tint: tint color (default is .white).
public func makeTransparent(withTint tint: UIColor = .white) {
setBackgroundImage(UIImage(), for: .default)
shadowImage = UIImage()
isTranslucent = true
tintColor = tint
titleTextAttributes = [NSAttributedStringKey.foregroundColor: tint]
}
}

Related

Xcode 13 - Navigation bar and status bar text color changing in swift OS 15

Recently I updated my Xcode to 13 and after that, I am facing some issues with the navigation bar and Status bar. I am using the tab bar in my view controller. After updating the Xcode, according to the version, I added some code related to the navigation bar.
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor(red: 58/255,green: 24/255, blue: 93/255, alpha: 1.0)
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
// Customizing our navigation bar
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.barTintColor = .white
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
}
Everything is working when I first opened the app. When I click on the other tab and then this tab. The status bar text color is changing.
I tried different ways to set the status bar text color. But nothing worked for me.
Use this function:
extension YourViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
}
If you just want to set to white colour you status bar in the entire project you can easily do it by adding the following keys into your project.plist file:
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
So, what does these keys do... You are just defining that you status bar colour will not be controlled by the any view controller and that the default colour will be "light theme" that is pretty much the white colour that you want in this case...
this would be the best approach if you do not intent to update the status bar colour all over the place.
this was tested and is working as expected on Xcode 13 and iOS [13.x,14.x,15.x]
If you want to update to customise or update the colours for the navigation bar you would should be able to do with an extension like this one bellow, just few examples but you guys should get what I mean:
extension UINavigationBar {
func clearBackground() {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithTransparentBackground()
navBarAppearance.shadowColor = UIColor.clear
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
self.standardAppearance = navBarAppearance
self.scrollEdgeAppearance = navBarAppearance
}
func blackColor() {
// Create an Appearance and customise as you wish.
// in this case just a black background with titles in white.
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.shadowColor = UIColor.black
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
self.standardAppearance = navBarAppearance
self.scrollEdgeAppearance = navBarAppearance
}
}
with this code you should be able to easy update on any view controller by just calling something like:
override func viewDidLoad() {
super.viewDidLoad()
// set the navigation bar color as transparent
self.navigationController?.navigationBar.clearBackground()
}

How to change the background color of navigationBar

I am trying to change the background color of the navigation bar to black but without success. I have checked the answer to this related question but none of the solutions have worked. This is what I have tried:
navigationController?.navigationBar.backgroundColor = UIColor.black
UINavigationBar.appearance().backgroundColor = UIColor.black
You can also change the navigation bar color for a specific view controller, this way:
extension UIViewController {
func setCustomNavigationColor(color: UIColor = .black, isTranslucent: Bool = false ){
self.navigationController?.navigationBar.barTintColor = color
self.navigationController?.navigationBar.isTranslucent = isTranslucent
}
}
call this from viewDidLoad()
setCustomNavigationColor()
Try this:
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().isTranslucent = false
Note: This changes the bar colour for the entire app.

SwiftUI - TabView with NavigationView generates gray area

I have some problems with my tabbed view when I set isTranslucent to false in combination with a NavigationView.
Does anyone know how to fix this? The problem is shown in the attached image.
I need translucent set to false otherwise I can't get the dark color.
You can set backgroundColor. Don't set isTranslucent to false or it will create these artefacts you mentioned.
UITabBar.appearance().backgroundColor = .black
UINavigationBar.appearance().backgroundColor = .black
It becomes much darker. It isn't completely opaque though.
Edit: Just watched Modernizing Your UI for iOS 13 This is the way to do it :
The TabView and NavigationView are actually UIHostedController for the legacy UITabBarController and UINavigationController:
let appearance = UITabBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor .white]
Then set the appearance on the various type of appearance.
tabBar.standardAppearance = appearance
2nd Edit:
extension UINavigationController {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
navigationBar.standardAppearance = appearance
navigationBar.compactAppearance = appearance
navigationBar.scrollEdgeAppearance = appearance
}
}
extension UITabBarController {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let appearance = UITabBarAppearance()
appearance.configureWithOpaqueBackground()
tabBar.standardAppearance = appearance
}
}
There should be a cleaner way to get to both tabBar and navBar.
Reference: https://developer.apple.com/videos/play/wwdc2019/224/
I was using UIKit with SwiftUI. My Tab bar was creating in storyboard but the view for which I was getting extra bottom space was a swiftui view as you mentioned. I tried all above solution but Nothing worked for me.
I am using Xcode 12.4 for development. My solution is to mark Translucent to true in storyboard and Bottom extra gray bar was gone.
Just customize it in an extension like this:
extension UITabBarController {
override open func viewDidLoad() {
super.viewDidLoad()
let appearance = UITabBarAppearance()
appearance.backgroundColor = .black
tabBar.standardAppearance = appearance
}
}
Pay attention that the overridden function must be viewDidLoad(). At least it doesn't work for me when it is a viewDidAppear(:) function.
It's easier than all that, just delete the next line:
UITabBar.appearance().isTranslucent = false

How to overlay navigationBar with TableViewController?

I've set my navigationBar background invisible using the following code:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.barStyle = .black
But my tableView had an indent and I've lifted it to the top by this:
if #available(iOS 11.0, *) {
self.tableView.contentInsetAdjustmentBehavior = .never
}
else {
self.automaticallyAdjustsScrollViewInsets = false
}
The problem is that I have a tabBar and now tabBar overlays my tableView too
How can I set contentInsetAdjustmentBehavior only for top? Or should I use another way for lifting tableView and make navigationBar invisible?

UINavigationController NavigationBar not applying transparent image

When applying transparent image in the navigation tab bar. It's turning white instead of being transparent..
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.tintColor = UIColor.white
Ensure that something is under the navigation bar so that it does not just become transparent showing the white background. You will need to drag uiviews under it and then add layout constraints. Otherwise, make sure you check that you have not changed the color of the navigation bar elsewhere.
Use this extension to make navigation bar transperant.
extension UINavigationController {
func transparant() {
self.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
self.view.backgroundColor = UIColor.clear
}
}
if you are using Navigation Controller try this:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
title = "Some Title"
if you are using UINavigationBar try this:
#IBOutlet var navBarOutlet: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
// navigatin bar transparent
navBarOutlet.setBackgroundImage(UIImage(),for:.default)
navBarOutlet.shadowImage=UIImage()
navBarOutlet.topItem?.title = "Some Title"
}