Swift - Title Not Appearing for Navigation View Controller

为君一笑 提交于 2019-12-12 08:44:34

问题


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:

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)

                }
            }


        }


    }



}

回答1:


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




回答2:


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"

}



回答3:


Try this:

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



回答4:


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" 
}



回答5:


In the Logout Screen viewcontroller add this -

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



回答6:


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 

}



回答7:


Swift 5 Simple answer please follow that

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


来源:https://stackoverflow.com/questions/29676757/swift-title-not-appearing-for-navigation-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!