【问题标题】:Swift - Title Not Appearing for Navigation View ControllerSwift - 导航视图控制器的标题未出现
【发布时间】:2015-06-22 23:31:09
【问题描述】:

我有一个导航视图控制器,它在操作时会向标签栏视图控制器发送一个 segue。由于这种 segue,选项卡式视图控制器继承了导航栏。我正在尝试将标题应用于附加到选项卡栏视图控制器的一个视图控制器,但通过代码设置标题对我不起作用。有谁知道这可能的原因吗?

这是我的故事板的图片:

带有注销按钮的视图控制器是我尝试在导航栏中设置标题的地方(代码):

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)

    }

}

嵌入在导航控制器中的视图控制器触发标签栏控制器的segue:

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)

                }
            }


        }


    }



}

【问题讨论】:

    标签: ios xcode swift uinavigationcontroller xcode-storyboard


    【解决方案1】:

    Swift 5 简单回答请遵循

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

    【讨论】:

      【解决方案2】:

      检查带有和不带 Tabbarcontroller 的 ViewController 的可用性注释行

      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 
      
      }
      

      【讨论】:

        【解决方案3】:

        这对我有用:self.parent?.title = "nav bar title"

        【讨论】:

          【解决方案4】:

          试试这个:

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

          【讨论】:

            【解决方案5】:

            SWIFT 3

            如果嵌入在 UINavigationController 中,您可以在 ViewController 的 viewDidLoad() 方法中按如下方式设置标题。

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

            【讨论】:

              【解决方案6】:

              在注销屏幕视图控制器中添加这个 -

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

              【讨论】:

                【解决方案7】:

                在您的视图控制器层次结构中,导航栏显示UITabBarController 的标题,而不是UITabBarController 内的视图控制器。

                在导航栏中显示标题的最简单方法是

                override func viewWillAppear(animated: Bool) {
                
                    self.tabBarController?.navigationItem.title = "Profile Settings"
                
                }
                

                【讨论】:

                • 此解决方案似乎无法解决问题。我的导航栏实际上并没有出现。可能是因为我在viewDidLoad 中调用了self.navigationItem.leftBarButtonItem = nil 吗?
                • 抱歉忽略最后一条评论。我已将 navigationController 设置为 false。假设我有一个非导航控制器 segue 到选项卡栏控制器。我会在非导航控制器的 segue 操作上设置此命令吗?
                猜你喜欢
                • 2012-06-09
                • 1970-01-01
                • 1970-01-01
                • 2015-07-12
                • 2019-04-03
                • 2014-10-09
                • 2018-11-09
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多