Reputation: 362
I am having a real hard time trying to get all of my navigation bar elements to show in my app I am creating. As you can see the right button appears just fine but my titles and my back button do not show. If I change the code below for my backButtonItem
to leftBarButton
it will work fine.
Believe me when I say that I have tried going through several SO posts even when I was creating this I was looking up the related posts as well and I couldn't find anything that worked.
Here is my UINavigationController
class:
import UIKit
class NavViewController: UINavigationController, UINavigationBarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
let navigationBar = UINavigationBar(frame: CGRectMake(0, 20, self.view.frame.size.width, 44)) // Offset by 20 pixels vertically to take the status bar into account
navigationBar.barTintColor = UIColor(red: 0.0627, green: 0.4862, blue: 0.0627, alpha: 1)
navigationBar.delegate = self;
// Create a navigation item with a title
let navigationItem = UINavigationItem()
//Create the Back Button
let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
backButton.tintColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1)
navigationItem.backBarButtonItem = backButton
//Create the Right Button (Go Home) Button
let rightButton = UIBarButtonItem(title: "Home", style: UIBarButtonItemStyle.Plain, target: self, action: "goHome")
rightButton.tintColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1)
navigationItem.rightBarButtonItem = rightButton
//Change the Navigation Bar Title Color
navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Any help you can give me would be greatly appreciated. Thanks!!!!
EDIT
So I went ahead and added this to my appdelegate.swift and I can get the Back Button to show the way I want along with my Title(s) but not my Right Button does not show.
import UIKit
@UIApplicationMain class AppDelegate : UIResponder, UIApplicationDelegate {
var window : UIWindow?
var navigationItem = UINavigationItem()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window!.backgroundColor = UIColor(red: 0.2078, green: 0.2078, blue: 0.2078, alpha: 1)
//Create the Back Button
let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
navigationItem.backBarButtonItem = backButton
//Create the Right Button (Go Home) Button
let rightButton = UIBarButtonItem(title: "Home", style: UIBarButtonItemStyle.Plain, target: self, action: "goHome")
navigationItem.rightBarButtonItem = rightButton
// Assign the navigation item to the navigation bar
UINavigationBar.appearance()?.items = [navigationItem]
//Change the Navigation Bar Color
UINavigationBar.appearance()?.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
UINavigationBar.appearance()?.tintColor = UIColor.whiteColor()
UINavigationBar.appearance()?.barTintColor = UIColor(red: 0.0627, green: 0.4862, blue: 0.0627, alpha: 1)
return true
}
func goHome() {
var rootViewController = self.window!.rootViewController as UINavigationController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Home") as HomeViewController
rootViewController.popToViewController(homeViewController, animated: true)
}
}
Upvotes: 0
Views: 6452
Reputation: 2928
As per your question it seems that you don’t want to set left and right bar button in an individual call and u require to get both of them by writing it once and get them in all other class. Here’s what you need to do.
In AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
UINavigationBar.appearance().barTintColor = UIColor(red: 0.0627, green: 0.4862, blue: 0.0627, alpha: 1)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
return true
}
The above code will set this attributes for navigation bar throughout the application.
Now, Add New File and name it as RootViewController - “Subclass of - UIViewController”.
import UIKit
class RootViewController: 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.
}
func setLeftBarButton() {
// Below line will Create Custom leftBarButton.
var backBtn = UIBarButtonItem(title: "First", style: UIBarButtonItemStyle.Bordered, target: self, action: "popBack") as UIBarButtonItem
self.navigationItem.leftBarButtonItem = backBtn
// Below line will Remove default backBarButton.
self.navigationItem.backBarButtonItem = nil;
}
func setRightBarButton() {
let rightBtn: UIBarButtonItem = UIBarButtonItem(title: "Home", style: UIBarButtonItemStyle.Bordered, target: self, action: "goToNext")
self.navigationItem.rightBarButtonItem = rightBtn
}
func popBack() {
self.navigationController?.popViewControllerAnimated(true)
}
func goToNext() {
print("Go to Next View.")
}
Now In all your View Controller you just need to do is “ Replace - UIViewController with RootViewController”.
Now in Second View Controller all you need to do is call methods from RootViewController in ViewDidLoad of SecondViewController.
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Second View"
setLeftBarButton()
setRightBarButton()
// Do any additional setup after loading the view.
}
Output :
Hope this saves your problem.
Upvotes: 2
Reputation: 987
@KyleMassacre, UINavigationController is used for navigation from one controller to another controller UINavigationController Apple Doc
It show automatically back button.
In your AppDelegation you only need:
//Change the Navigation Bar Color
UINavigationBar.appearance()?.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
UINavigationBar.appearance()?.tintColor = UIColor.whiteColor()
UINavigationBar.appearance()?.barTintColor = UIColor(red: 0.0627, green: 0.4862, blue: 0.0627, alpha: 1)
In a Example with 2 View Controllers. Button "Push" from the first View Controller to SecondViewController:
The code in SecondViewController:
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let rightButton = UIBarButtonItem(title: "Home", style: UIBarButtonItemStyle.Plain, target: self, action: "goHome")
rightButton.tintColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1)
navigationItem.rightBarButtonItem = rightButton
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
func goHome() {
// navigation where you want. You can use:
//navigationController?.pushViewController(<#viewController: UIViewController#>, animated: <#Bool#>)
//navigationController?.popToRootViewControllerAnimated(animated: Bool)
//presentViewController(<#viewControllerToPresent: UIViewController#>, animated: <#Bool#>, completion: <#(() -> Void)?##() -> Void#>)
}
}
And the result:
Upvotes: 1