How to commit changes of navigator bar translucency properly? - ios

I have faced with such problem.
I have two VC. One of it has basic navigator bar, which inherits from main Navigator bar.
In this nav bar I use such settings to customise my nav nar
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationBar.createGradientNavigatorBar()
navigationBar.isTranslucent = false
navigationBar.barTintColor = UIColor.init(red: 0/255, green: 97/255, blue: 185/255, alpha: 1)
navigationBar.shadowImage = UIImage()
}
On the second VC I have to set custom gradient background which connected with view. For this purpose I have extended my view up to bar height and make par transculent. Here is code:
override func viewWillAppear(_ animated: Bool) {
isCurrentViewControllerAppropriate.shared.isCurrentVcAppropriate = isCurrentViewController()
let firstColor = UIColor.init(red: 0/255, green: 94/255, blue: 198/255, alpha: 1)
let secondColor = UIColor.init(red: 27/255, green: 145/255, blue: 226/255, alpha: 1)
profileBackgroundView.createGradient(firstColor: firstColor.cgColor, secondColor: secondColor.cgColor)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.backgroundColor = UIColor.clear
navigationController?.navigationBar.isTranslucent = true
}
override func viewWillDisappear(_ animated: Bool) {
navigationController?.navigationBar.isTranslucent = false
}
Everything is working fine, but I have rough transition between vc. I've attached.
As you may notice when I return to my first VC is loading it have an artefact with navigator bar color from second VC, which resolves after a certain period of time. I have no idea how to deal with it
I'm not sure whether my approach is the right one, so I'll be glad to hear any advices consider solving this issue.

Related

When I segue another page, the top of the navigation bar turns black SWIFT 5

When I click on the search icon, I segue another page and added the search bar to the navigation bar.But after making a segue, the top of the navigation bar turns black.
Before segue:
After segue:
class SearchViewController: UIViewController, UISearchBarDelegate {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
setupSearch()
}
func setupSearch() {
searchController.searchBar.backgroundColor = #colorLiteral(red: 0.1333333333, green: 0.1333333333, blue: 0.1333333333, alpha: 1)
navigationController?.navigationBar.backgroundColor = #colorLiteral(red: 0.1333333333, green: 0.1333333333, blue: 0.1333333333, alpha: 1)
navigationItem.searchController = searchController
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
searchController.searchBar.delegate = self
}
}
Try adding edge for extended layout either from code like below
self.navigationController?.navigationBar.isTranslucent = true
self.edgesForExtendedLayout = UIRectEdge.all
Or You can do it from the XIB/Storyboard as well.
storyboard -> navigation controller - > untick the shows navigation bar and run again.
I solved the problem by adding backgroundcolor to my view.
view.backgroundColor = #colorLiteral(red: 0.1333333333, green: 0.1333333333, blue: 0.1333333333, alpha: 1)
Then I added another white view. I don't know if this is the right way, but the problem is solved.

UINavigationBar tintColor is misbehaving

I have issue when set UINavigationBar tintColor.
It is misbehaving
I'm using xcode 11.3.1, swift 5, iOS 13.3
*MyClass
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.title = "test"
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
}
*result
real device
https://imgur.com/sONr4vq
simulator
https://imgur.com/vs5lhgR
I want to set back button color like title
I only get error on real device
Please help me
Thank you
To set back button and hide "back":
let backItem = UIBarButtonItem()
backItem.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
navigationItem.backBarButtonItem = backItem
You should but this code to the ViewController before push the new one.
Try access to navigationController.navigationBar instead of navigationController.view :
self.navigationController?.navigationBar.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
Did you try using UIColor instead of #colorLiteral?
Like
self.navigationController?.navigationBar.tintColor = UIColor(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
//or
self.navigationController?.navigationBar.tintColor = UIColor(displayP3Red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
Edit:
Since that the above did not work for you, did you try this?
self.navigationController?.navigationBar.tintColor = self.navigationController?.navigationItem.titleView?.backgroundColor
Considering that all you want is to have both the button and title to have the same colour right?

Navigation bar changes upon returning to previous VC

I'm having a few issues with my navigation bar styling. Let's say I have two view controllers.
VC 1 has an orange bar with white text that I have set up like so:
func stylingStuffs(){
self.navigationController?.navigationBar.barTintColor = UIColor(red: 0.94, green: 0.38, blue: 0.24, alpha: 1)
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 0.94, green: 0.38, blue: 0.24, alpha: 1)
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
self.navigationController?.navigationBar.isTranslucent = false
UIApplication.shared.statusBarStyle = .lightContent
}
VC 2 has a transparent navigation bar and the code for that is:
func stylingStuffs(){
navigationController?.setNavigationBarHidden(false, animated: true)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.barTintColor = UIColor(red: 0.94, green: 0.38, blue: 0.24, alpha: 0)
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 0.94, green: 0.38, blue: 0.24, alpha: 0)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.barStyle = .default
UIApplication.shared.statusBarStyle = .lightContent
}
When I navigate from VC1 to VC2 everything works fine, when I navigate from VC2 to VC1, VC1's Navbar is now white/transparent as well as the status bar.
How can I fix this or I guess reset the styles upon return to VC1?
first view VC 1 controller viewDidAppear call stylingStuffs()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated: animated)
stylingStuffs()
}
#rmaddy comment
You are confused because there is actually only one navigation bar. It is part of the one navigation controller that both view controllers are in
so need to reset navigation bar style for VC 1

UINavigationBar won't show again after hiding on scroll/swipe

As the title says, the navigationBar doesn't show up after hiding it on scroll. I tried setting it in xCode as well as programmatically. Either way, the problem is still there.
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
navigationController?.hidesBarsOnSwipe = true
}
Here is what it looks like after hiding the navBar. Note that the status bar color also changed color from white to black.
In viewDidLoad I have :
//MARK: navigation bar styles
self.navigationController?.navigationBar.backgroundColor = UIColor.init(red: 26/255, green: 24/255, blue: 24/255, alpha: 1)
self.navigationController?.navigationBar.clipsToBounds = true
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont(name:"GillSans", size: 20)!]
Does anyone have a good solution for this? Thank you in advance for your help!
Try this code:
Note : As you setting your navbar background close to black. You have to change your status bar content to light.
//Update your plist with below code
View controller-based status bar appearance = NO
In your ViewController:
title = "Some Title"
navigationController?.navigationBar.barTintColor = UIColor(red: 26/255, green: 24/255, blue: 24/255, alpha: 1)
navigationController?.navigationBar.tintColor = UIColor.white
//Title Colour and Font
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white, NSFontAttributeName:UIFont(name:"Arial", size: 20)!]
navigationController?.navigationBar.clipsToBounds = false
UIApplication.shared.statusBarStyle = .lightContent
}
override func viewDidAppear(_ animated: Bool) {
navigationController?.hidesBarsOnSwipe = true
}
output:

PushViewController & set navigation bar color and title

I made a push function in a button:
#IBAction func prodButton(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var secondViewController = CollectionViewController()
secondViewController = storyboard.instantiateViewControllerWithIdentifier("CollectionViewController") as! CollectionViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
}
This button pushes to the secondViewController but when I looked at the navigation bar of the second view controller I noticed that it has set up a back button automatically. The problem is that this back button's color is light blue and it doesn't fit with my design. I tried to change it like that in viewDidAppear
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.redColor()
and also the bar color:
self.navigationController?.navigationBar.barTintColor = UIColor(red: 65, green: 61, blue: 116, alpha: 1.0)
but there wasn't any change.
I'd be really thankful if somebody help me with that.
Thanks in advance.
Try to set your color one time, it will be the same color for everywhere after:
let navBar = UINavigationBar.appearance()
navBar.tintColor = UIColor.redColor()
You should put this code in AppDelegate. You could also set the barTint in Storyboard if you use it.
Use
//Func Alter color navigation bar
func AlteraCorNavigationBar(Navigation:UINavigationController){
Navigation.navigationBar.tintColor = CorTextoNavigationBar()
//Navigation.navigationBar.barTintColor = CorPredominante()
Navigation.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : CorTextoNavigationBar(), NSFontAttributeName : UIFont(name: "ArialHebrew-Light", size: 22)!]
}
//func CorPredominante()->UIColor{
//return UIColor.redColor()
//(rgba: "#ecf0f1")
// YOU CAN LIBRARY COLOR RGBA [HERE][1] AND USE UIColor(rgba: "#b6011a")
//}
func CorTextoNavigationBar()->UIColor{
return UIColor(red: 65, green: 61, blue: 116, alpha: 1.0)
}
for called in view controller:
class NewViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
AlteraCorNavigationBar(navigationController)
}
}
I found the mistake.
The colors shoud be devided by 255:
UINavigationBar.appearance().tintColor = UIColor(red: 149.0 / 255.0, green: 148.0 / 255.0, blue: 192.0 / 255.0, alpha: 0.5)
UINavigationBar.appearance().barTintColor = UIColor(red: 65.0 / 255.0, green: 61.0 / 255.0, blue: 116.0 / 255.0, alpha: 1.0)
Thank you all for the help ;)

Resources