cphill
cphill

Reputation: 5924

Swift - Title Not Appearing for Navigation View Controller

I have a navigation view controller that upon action sends a segue to a tab bar view controller. Because of this segue the tabbed view controller inherits the navigation bar. I am trying to apply a title to one of my view controllers attached to the tab bar view controller, but setting the title via code is not working for me. does anyone know the reason why that could be?

Here is a picture of my storyboard:

storuboard

The view controller with the logout button is where I am trying to set the title in the nav bar (code):

import UIKit

class ProfileSettingsViewController: UIViewController {

    override func viewWillAppear(animated: Bool) {

        self.navigationItem.title = "Profile Settings"

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.leftBarButtonItem = nil


    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    @IBAction func logoutButton(sender: AnyObject) {

        PFUser.logOut()
        var currentUser = PFUser.currentUser()
        self.performSegueWithIdentifier("userLoggedOut", sender: self)
        self.navigationController?.setNavigationBarHidden(self.navigationController?.navigationBarHidden == false, animated: true)

    }

}

View controller embedded in the navigation controller triggering the segue to the tab bar controller:

import UIKit

class UserRegistrationViewController: UIViewController {


    func displayAlert(title:String, error:String) {

        var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: {
            action in



        }))

        self.presentViewController(alert, animated: true, completion: nil)


    }

    @IBOutlet var usernameTextField: UITextField!

    @IBOutlet var emailTextField: UITextField!

    @IBOutlet var passwordTextField: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func registerUser(sender: AnyObject) {

        var error = ""

        if usernameTextField.text == nil || emailTextField.text == nil || passwordTextField.text == nil {

            error = "Please enter a username, email and password"

        }


        if error != "" {

            displayAlert("Error In Form", error: error)

        } else {

            var user = PFUser.currentUser()

            user.username = usernameTextField.text
            user.password = passwordTextField.text
            user.email = emailTextField.text

            user.saveInBackgroundWithBlock {
                (succeeded: Bool!, signupError: NSError!) -> Void in
                if signupError == nil {

                    println(user.username)
                    println(user.password)
                    println(user.email)


                    self.performSegueWithIdentifier("successfulRegistration", sender: self)

                    self.navigationController?.setNavigationBarHidden(self.navigationController?.navigationBarHidden == false, animated: true)

                    // Hooray! Let them use the app now.
                } else {

                    if let errorString = signupError.userInfo?["error"] as? NSString {
                        error = errorString
                    } else {

                        error = "Please try again later."

                    }


                    self.displayAlert("Could Not Sign Up", error: error)

                }
            }


        }


    }



}

Upvotes: 11

Views: 29543

Answers (8)

quynhbkhn
quynhbkhn

Reputation: 752

Add this code into your viewcontroller:

// title color
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]

It's working for me. I hope this help for someone.

Upvotes: 0

Shakeel Ahmed
Shakeel Ahmed

Reputation: 6021

Swift 5 Simple answer please follow that

self.title = "Your Title"

or 

DispatchQueue.main.async {
    self.title = "Your Title"
}

Upvotes: 4

Upendranath Reddy
Upendranath Reddy

Reputation: 389

Check commented lines for usability ViewController with and without Tabbarcontroller

override func viewDidAppear(_ animated: Bool) {
    addNavBarImage()
}

func addNavBarImage() {

    let navController = self.navigationController!
    let image = #imageLiteral(resourceName: "navview") //Your logo url here
    let imageView = UIImageView(image: image)
    let bannerWidth = navController.navigationBar.frame.size.width
    let bannerHeight = navController.navigationBar.frame.size.height
    let bannerX = bannerWidth / 2 - (image.size.width) / 2
    let bannerY = bannerHeight / 2 - (image.size.height) / 2
    imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
    imageView.contentMode = .scaleAspectFit


    self.navigationItem.titleView = imageView //  ViewController without Tabbarcontroller 

    self.tabBarController?.navigationItem.titleView = imageView //ViewController with Tabbarcontroller 

}

Upvotes: 0

Rehaan
Rehaan

Reputation: 246

This worked for me: self.parent?.title = "nav bar title"

Upvotes: 23

Hsm
Hsm

Reputation: 1540

Try this:

self.navigationController?.navigationBar.topItem?.title = "Profile Settings"

Upvotes: 10

AWebster
AWebster

Reputation: 141

SWIFT 3

If embedded in a UINavigationController, you can set the title as follows from within your ViewController's viewDidLoad() method.

override func viewDidLoad() {
    super.viewDidLoad()

    /* If view controller is a direct child of UINavigationController */
    self.title = "Title Here"

    /* If view controller's parent is a direct child of UINavigationController e.g. a child of embedded tab view controller */
    self.parent?.title = "Title Here" 
}

Upvotes: 7

Uttam Sinha
Uttam Sinha

Reputation: 722

In the Logout Screen viewcontroller add this -

self.tabBarController?.navigationItem.title="Profile Settings"

Upvotes: 5

Jakub Vano
Jakub Vano

Reputation: 3873

In your view controller hierarchy, navigation bar is displaying the title of UITabBarController, not view controllers inside the UITabBarController.

Easiest way to get title shown in navigation bar would be

override func viewWillAppear(animated: Bool) {

    self.tabBarController?.navigationItem.title = "Profile Settings"

}

Upvotes: 21

Related Questions