Navigation Controller Black when trying to make transparent - ios

Completely stumped. I've looked all over and implemented every solution I could find. I can't seem to get the navigation bar to become transparent.
When trying to set the background color, I just get a black bar at the top. Same as if I try to set the background images. I've tried all of these and all of there many variations.
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.backgroundColor = UIColor.clear
I'm using it in
viewWillAppear() and in an animation when scrolling. The navigation bar is transparent, and then as you scroll navigation bar gets a white background with gray text.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.navigationController?.navigationBar.barStyle = .default
let offset = self.tableView.contentOffset.y
if offset > 250.0 {
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray]
self.navigationController?.navigationBar.topItem?.title = spot!.Name
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.tintColor = UIColor.darkGray
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.barTintColor = UIColor.white
}
else {
self.navigationController?.navigationBar.topItem?.title = nil
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.barTintColor = UIColor.white
self.navigationController?.navigationBar.shadowImage = UIImage()
}
}
I've also tried setting the background color to white, and changing the alpha = 0, but that doesn't work either.
Any help greatly appreciated.

This is because the window's background color is black and
You should set window?.backgroundColor = UIColor.white
in the AppDelegate application didFinishLauchingWithOptions method

Related

Black box when transitioning from custom large title nav bar to regular nav bar

There is a strange thing that occurs when I transition from one view with a large title navigation bar that has been customized with a different shadow to another view that has a regular height navigation bar. You can see the black box in this gif
Here is the related code that lives in both the main view and detail view
Main view:
func setupNavBar() {
// Set the nav bar to have large titles. This is on a per instance basis
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.tintColor = UIColor.black
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.layer.shadowColor = UIColor(red:0.87, green:0.87, blue:0.87, alpha: 0.3).cgColor
navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
navigationController?.navigationBar.layer.shadowRadius = 6.0
navigationController?.navigationBar.layer.shadowOpacity = 1.0
navigationController?.navigationBar.layer.masksToBounds = false
}
Detail view:
func setupNavBar() {
self.title = colorPalette?.title
navigationController?.navigationBar.prefersLargeTitles = false
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.tintColor = UIColor.black
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.layer.shadowColor = UIColor(red:0.87, green:0.87, blue:0.87, alpha: 0.3).cgColor
navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
navigationController?.navigationBar.layer.shadowRadius = 6.0
navigationController?.navigationBar.layer.shadowOpacity = 1.0
navigationController?.navigationBar.layer.masksToBounds = false
}
There is no view that has a black background in interface builder either. I'm not sure why this is happening.
Instad of
navigationController?.navigationBar.isTranslucent = false
use
navigationController?.navigationBar.isTranslucent = true
Check ✓ Extended Edges Under Opaque Bars on storyboard, or set
extendedLayoutIncludesOpaqueBars = true
Or set the navigationBar's translucent to true.
navigationController?.navigationBar.isTranslucent = true

iOS 11 navigation bar transparent with large titles

I have default preferences for uinavigationbar:
UINavigationBar.appearance().barTintColor = .red
UINavigationBar.appearance().titleTextAttributes = [ NSAttributedStringKey.foregroundColor:#colorLiteral(red: 1, green: 0.99997437, blue: 0.9999912977, alpha: 1)]
if #available(iOS 11.0, *) {
UINavigationBar.appearance().largeTitleTextAttributes = [ NSAttributedStringKey.foregroundColor:#colorLiteral(red: 1, green: 0.99997437, blue: 0.9999912977, alpha: 1)]
}
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().tintColor = #colorLiteral(red: 1, green: 0.99997437, blue: 0.9999912977, alpha: 1)
Structure: UINaviagtionController -> PostsTableViewController -> PostTableViewController.
I want to have large navigation bar on PostsViewController (with opaque background color) and transparent navigation bar on PostViewController.
In PostTableViewController i add:
self.navigationController?.navigationBar.backgroundColor = UIColor.clear
self.navigationController?.navigationBar.barTintColor = .clear
self.automaticallyAdjustsScrollViewInsets = false
//self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
//self.navigationController?.navigationBar.shadowImage = UIImage()
if #available(iOS 11.0, *) {
print("asd")
self.tableView.insetsContentViewsToSafeArea = false
self.tableView.contentInsetAdjustmentBehavior = .never
} else {
self.automaticallyAdjustsScrollViewInsets = false
}
Navigation bar is BLACK. I dont understand how make it transparent...
UPDATE:
git hub repository: https://github.com/Mazorati/testnavbar
i set isTranslucent = true, but large navigation becomes also transparent. Only default navigation is ok.
But i want navigationbar color to be opaque, so:
self.navigationController?.view.backgroundColor = .red
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.largeTitleDisplayMode = .automatic
As the result:
3D layers:
have you tried changing it to translucent?
self.navigationController?.navigationBar.isTranslucent = true
UPDATE:
You must remember that the navigation bar belongs to the UINavigationController which holds the views inside it, so that when you change it in one it changes for all.
The solution would be to change it to translucent in the second view controller's viewWillAppear method and change it to not translucent in the viewDidDissapear method. Play with the viewWill and viewDid appear methods until you find the right one for your needs.
Try this, this make your navigation bar transparent.
let img = UIImage()
navigationController?.navigationBar.shadowImage = img
navigationController?.navigationBar.setBackgroundImage(img, for: .default)

Transparent/translucent navigation bar with button items in Swift

I'm trying to get an almost transparent navigation bar. But I don't want the buttons on it to be transparent.
This is my code:
navigationController?.navigationBar.backgroundColor = UIColor.blackColor()
navigationController?.navigationBar.barTintColor = UIColor.blackColor()
navigationController?.navigationBar.translucent = true
navigationController?.navigationBar.alpha = 0.3
This does make it translucent but it also makes the buttons translucent/faded. How can I have a translucent bar but still have the buttons opaque?
Try this:
navigationController?.navigationBar.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3)
navigationController?.navigationBar.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3)
navigationController?.navigationBar.barTintColor = UIColor.blackColor()
navigationController?.navigationBar.translucent = true

remove black border from UISearchController search bar swift

I have a UISearchController in my tableView. Also note that there is a navigation item in the top.
My issue is that i have a black border in the top and bottom when I load the page, however, it is not present when I click the search bar.
Search bar on page load (with black border):
After clicking on search bar (no black border):
Here is the code related:
let searchController = UISearchController(searchResultsController: nil)
in viewDidLoad:
searchController.searchBar.barTintColor = UIColor.redColor()
searchController.searchBar.tintColor = UIColor.whiteColor()
I followed several similar questions and made the following changes after the above lines in viewDidLoad():
1) searchController.searchBar.backgroundImage = UIImage()
2) searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
3) searchController.searchBar.layer.borderColor = UIColor.clearColor().CGColor
4)
searchBar.layer.borderWidth = 1
searchBar.layer.borderColor = UIColor.whiteColor().CGColor
None worked. Is this an issue with the order I am using the code, or how would I get rid of these lines?
Fixed this after a lot of search. Here's how i did it. In viewDidLoad, added the following lines:
self.searchController.searchBar.translucent = false
self.searchController.searchBar.backgroundImage = UIImage()
self.searchController.searchBar.barTintColor = UIColor.redColor()
self.searchController.searchBar.tintColor = UIColor.whiteColor()
After that in app.delegate file in didFinishLaunchingWithOptions added the following code:
let backgroundColor = UIColor.redColor()
let foregroundColor = UIColor.whiteColor()
UIApplication.sharedApplication().statusBarStyle = .LightContent
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(backgroundColor.toImage(), forBarMetrics: UIBarMetrics.Default)
UINavigationBar.appearance().tintColor = foregroundColor
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: foregroundColor]
Also need this extension on the app.delegate file (place outside of the class)
extension UIColor{
func toImage() -> UIImage {
let rect = CGRectMake(0, 0, 1, 1)
UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
self.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
(Thanks to Noel for the extension taken from this answer)
And finally, the desired result:
I think it is the navigation bar you need to remove the border for.
How to hide iOS7 UINavigationBar 1px bottom line
For iOS 13 / Swift 5; the following line makes the trick:
UISearchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
Cheers!

Changing the Color of Navigation Bar in Swift

I have a MKMap in my ViewController and I allow my users to switch map types between "Standard", "Hybrid", and "Satellite" - much like in the Maps app. Like in the maps app I have a bottom bar, and then I have a navigation bar at the top. When the user switches between map types, the bars should change to a black background with white buttons for "Hybrid" and "Satellite", and a white background with blue buttons for "Standard.
I have the bottom bar changing colors correctly but am unable to get the navigation bar to change at all. Here is my function for when a user changes map types:
func changeMapType(sender:UISegmentedControl){
if mapType.selectedSegmentIndex == 0{
mapView.mapType = MKMapType.Standard
toolbar.barTintColor = UIColor(red:1.0, green:1.0, blue:1.0, alpha:1.0)
toolbar.tintColor = UIColor(red:0.0, green:122.0/255.0, blue:1.0, alpha:1.0)
self.navigationController?.navigationBar.translucent = false
self.navigationController?.navigationBar.barTintColor = UIColor(red:1.0, green:1.0, blue:1.0, alpha:1.0)
self.navigationController?.navigationBar.tintColor = UIColor(red:0.0, green:122.0/255.0, blue:1.0, alpha:1.0)
defaults.setValue("Standard", forKey: "initialMapType")
}
else if mapType.selectedSegmentIndex == 1{
mapView.mapType = MKMapType.Hybrid
toolbar.barTintColor = UIColor.blackColor()
toolbar.tintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.translucent = false
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
defaults.setValue("Hybrid", forKey: "initialMapType")
}
else if mapType.selectedSegmentIndex == 2{
mapView.mapType = MKMapType.Satellite
toolbar.barTintColor = UIColor.blackColor()
toolbar.tintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.translucent = false
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
defaults.setValue("Satellite", forKey: "initialMapType")
}
}
Does anyone know what I must do differently to get my nav bar to change colors?
It is very probably because your self.navigationController is empty. Try that first! It is o-so-very common problem. Maybe you are calling it from wrong view controller that in fact does not have NC. You can go around that problem by using .appearance() interface, like this:
To change bar tint color (background of navigation bar):
UINavigationBar.appearance().barTintColor = UIColor.whiteColor()
To change color of the texts:
UINavigationBar.appearance().titleTextAttributes = [UITextAttributeTextColor: UIColor.whiteColor()]
Be aware that this changes navigation bar for entire application (basically, appearance is the way how to change default setting), so it might not be appropriate. If you are interested, here you can read more about appearance.
Hope it helps!
Edit: Easy way how to check for empty navigation controller would be force-unwrapping the variable, self.navigationController!.navigationBar instead of ?, if your app crashes, your problem lies here (but don't tell to anyone I suggested that as it is not very slick solution how to find the problem, though fast)
Updated for Swift 3, 4, 4.2, 5+
// setup navBar.....
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Swift 4
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Swift 4.2, 5+
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Also can check here : https://github.com/hasnine/iOSUtilitiesSource

Resources