Is it possible to set the navigation bar color for just a single View Controller in the navigation hierarchy? Let the default navigation bar color be red and just the last view controller in the line should have a blue one. I've used these two lines to color the navigation bar of said view controller:
navigationController?.navigationBar.barTintColor = .blue
navigationController?.navigationBar.tintColor = .white
But when going back (e.g. by pressing the back button) the navigation bar stays blue. Setting the color back to red using above code doesn't do anything.
The navigationBar is shared across all the view controllers that are in the same UINavigationController stack.
If you want to change it's look for a specific view controller, you'll have to set the new style when the view controller is shown and remove it when the view controller is dismissed. This could be done in the viewWillAppear/viewWillDisappear of your view controller for example.
I could get the navigation bar to change colors coming from a ViewControllerB to ViewControllerA perfectly fine with your code. I am not sure what your initial problem was. Here is my code which worked:
ViewController A:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barTintColor = .red
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func buttonAction(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "Second")
//self.present(controller, animated: true, completion: nil)
self.navigationController?.pushViewController(controller, animated: true)
}
}
ViewController B:
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barTintColor = .blue
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
It worked without an issue.
Related
There are many answers in SO that provide solutions for hiding the navigation bar shadow. Those work for me except for this particular case, which I'm describing here. Therefore, this question is not a duplicate.
To test this particular case, I created a new project using the master-detail app template. In the DetailViewController -> viewDidAppear, I coded the following:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
The above code works for a Single View App and on iPad Air 2 simulator. However, it doesn't work on the detailViewController of a master-detail app in iPhoneX simulator.
Alternatively, I also tried retrieving the subviews of the navigationBar in viewDidAppear and tried hiding the shadow (see code below). However, the subview count is zero. How could that be?
for parent in self.navigationController!.navigationBar.subviews {
for childView in parent.subviews {
if(childView is UIImageView) {
childView.removeFromSuperview()
}
}
}
Any help on this is much appreciated.
For Generic flow
You could use this setup. Say ViewController is the all other view controller class (where you want the shadow), and DetailViewController is the detail view controller class.
The thing i'm doing preserving the shadow image.
ViewController.swift
class ViewController: UIViewController {
var shadowImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
shadowImage = self.navigationController?.navigationBar.shadowImage
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.shadowImage = shadowImage
}
}
And DetailViewController.swift
class DetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.navigationController?.navigationBar.shadowImage = UIImage()
}
}
Storyboard setup
Output
Note: Another neat approach would be storing the shadow within the DetailsViewController and setting it while the view is about to disappear
class DetailsViewController: UIViewController {
var shadowImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
shadowImage = self.navigationController?.navigationBar.shadowImage
self.navigationController?.navigationBar.shadowImage = UIImage()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.shadowImage = shadowImage
}
}
This solution is more elegant and resulting in a clean management.
For MasterDetailFlow, Using SplitViewController
In your MasterViewControlelr.swift
override func viewWillAppear(_ animated: Bool) {
clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed // placeholder code when you created the project
super.viewWillAppear(animated)
self.navigationController?.navigationBar.shadowImage = nil
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.shadowImage = UIImage()
}
In your DetailViewController.swift
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.shadowImage = UIImage()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.shadowImage = nil
}
Output(Master/Detail flow)
I want to change the appearance of all Back buttons in the app by setting the text to "Back" and removing the arrow (even just removing the arrow would be fine). I'm trying to find a way to do it globally for all view controllers in the app while also keeping the functionality (I don't want to create a new instance of a UIBarButtonItem and having to set the selector).
I created a custom UINavigationController and tried to set the back button title there with navigationItem.backBarButtonItem?.title = "Back", but it didn't work. Any suggestions how to do it properly?
Just create CustomNavigationController then at
override open func pushViewController(_ viewController: UIViewController, animated: Bool) do controlClearBackTitle method
then use CustomNavigationController to fix it all in your app
import UIKit
class CustomNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override open func pushViewController(_ viewController: UIViewController, animated: Bool) {
controlClearBackTitle()
super.pushViewController(viewController, animated: animated)
}
override open func show(_ vc: UIViewController, sender: Any?) {
controlClearBackTitle()
super.show(vc, sender: sender)
}
func controlClearBackTitle() {
self.navigationBar.backIndicatorImage = UIImage()
self.navigationBar.backIndicatorTransitionMaskImage = UIImage()
topViewController?.navigationItem.backBarButtonItem = UIBarButtonItem(title: "AnyTitle", style: .plain, target: nil, action: nil)
}
}
I think something like this should do it. Try putting it in your custom NavigationController.
let backImage = UIImage(named: "backImage")
self.navigationController?.navigationBar.backIndicatorImage = backImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage
EDIT
Sorry totally forgot about title. This should do it:
self.navigationController?.navigationBar.backItem?.title = "New title"
I have structure:
*- TabBarViewController (Root)
*-- NavigationViewController
*---- ChatViewController
*-- NavigationViewController
*---- MenuViewController
and while I'm switching tabbar items, viewWillAppear in (Chat, Menu) called only once, but in NavigationVC called every times i switch.
Is it possible to call automatically viewWillAppeare in Chat and Menu ViewControllers while switching items?
super.viewWillAppear is inside method.
my code looks like:
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let v1 = storyboard!.instantiateViewController(withIdentifier: "ChatViewController")
let v2 = storyboard!.instantiateViewController(withIdentifier: "MenuViewController")
viewControllers = [v1,v2]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
class ChatViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print(self,#function)
}
}
class MenuViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print(self,#function)
}
}
It works on clear new project but on old ( where im working, and i have loot of funcionality, doesn't work)
StoryboardId is linked to NavigationViewController in Storyboard
I found issue:
In extension UINavigationController i have method
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if !UserDefaults.standard.isUserPresented {
navigationBar.barTintColor = .rgbColor(red: 43, green: 43, blue: 43, alpha: 1)
} else {
navigationBar.barTintColor = .rgbColor(red: 100, green: 100, blue: 100, alpha: 1)
}
}
and this block viewWillAppear in child view controllers in NavigationBar
Your question is not clear about adding viewcontroller to tabbarcontroller and navigation controller . I have created everything in storyboard . View will appear in view controller are :
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("Menu View will appear")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("Chat View will appear")
}
Am able to get below output when i switch :
Menu View will appear
Chat View will appear
Menu View will appear
Chat View will appear
I have a Nav View which when it pops out by tapping the burger menu then opens to half the page. It still has the other views (the root view controller and its children) in the background.
When a user taps the greyed out background area where the other views are they can interact with those views and the page navigates leaving the nav view over the top.
The Nav View is used in multiple places so I need the code for disabling interactions with other views to be in the Nav View Controller. Code below.
import UIKit
class MenuNavViewController: ENSideMenuNavigationController, ENSideMenuDelegate {
var tabBar: ManagerTabViewController!
override func viewDidLoad() {
super.viewDidLoad()
let sb = UIStoryboard(name: "iPhoneStoryboard", bundle: nil)
let menu = sb.instantiateViewController(withIdentifier: "MenuTableViewController") as! MenuTableViewController
menu.tabBar = self.tabBar
sideMenu = ENSideMenu(sourceView: self.view, menuViewController: menu, menuPosition: .left)
sideMenu?.bouncingEnabled = false
view.bringSubview(toFront: navigationBar)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - ENSideMenu Delegate
func sideMenuWillOpen() {
}
func sideMenuWillClose() {
}
func sideMenuDidClose() {
}
func sideMenuDidOpen() {
}
}
How can I disable/enable interaction with other views (or at least the view it's launched from) in the NavViewController above?
I found the answer shown in the code below.
Looking further into the ENSideMenuNavigationController class I found that the "init" method sets the relevant view controllers on "viewControllers" and implemented the code below which gave the desired effect.
import UIKit
class MenuNavViewController: ENSideMenuNavigationController, ENSideMenuDelegate {
var tabBar: ManagerTabViewController!
override func viewDidLoad() {
super.viewDidLoad()
let sb = UIStoryboard(name: "iPhoneStoryboard", bundle: nil)
let menu = sb.instantiateViewController(withIdentifier: "MenuTableViewController") as! MenuTableViewController
menu.tabBar = self.tabBar
sideMenu = ENSideMenu(sourceView: self.view, menuViewController: menu, menuPosition: .left)
sideMenu?.bouncingEnabled = false
sideMenu?.delegate = self
view.bringSubview(toFront: navigationBar)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - ENSideMenu Delegate
func sideMenuWillOpen() {
}
func sideMenuWillClose() {
}
func sideMenuDidClose() {
// Enable interaction with other views again
for viewController in self.viewControllers {
viewController.view.isUserInteractionEnabled = true
}
}
func sideMenuDidOpen() {
// Disable interaction with other views
for viewController in self.viewControllers {
viewController.view.isUserInteractionEnabled = false
}
}
}
Any other suggestions for better practice/code would still be appreciated.
I being searching this issue in stack overflow but couldn't get an exact answer to the issue, i being stuck in it for long time.
Mine issue is i'm trying to push a TestViewController through navigation controller. when i click the button the TestViewController is being load with navigation bar and the UIScreen is in black colour.
Code of TestViewController
import UIKit
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
navigationItem.title = "Test page"
}
}
Code of Button
#IBAction func secondButtonClicked(sender: AnyObject) {
buttonPressedNumber = "Two Clicked"
buttonTextColor = UIColor.magentaColor()
let a = TestViewController()
let b:UIViewController = a as UIViewController
navigationController?.pushViewController(b, animated: false)
}
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
self.navigationController?.pushViewController(resultViewController, animated: true)
You have not set the background color of your view.
The default color of the of UIWindow is Black. So if you have not set any other background colors in the stack they will all be transparent.
Not setting an appropriate background color for your UIViewController's view will also cause weird visuals during a transition.
let nextVC = self.storyboard?.instantiateViewControllerWithIdentifier("storyboardID") as! viewController
self.navigationController?.pushViewController(nextVC, animated: true)
not sure the background colour is set to white these days.
therefore, if view controller is created programmatically
in viewDidLoad/viewWillAppear for a white background colour
view.backgroundColor = UIColor.whiteColor()