Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 导航栏内容不显示_Swift_Uinavigationcontroller_Xcode6_Uinavigationbar - Fatal编程技术网

Swift 导航栏内容不显示

Swift 导航栏内容不显示,swift,uinavigationcontroller,xcode6,uinavigationbar,Swift,Uinavigationcontroller,Xcode6,Uinavigationbar,我真的很难让我所有的导航栏元素都显示在我正在创建的应用程序中。正如你所看到的,右边的按钮看起来很好,但是我的标题和后退按钮没有显示出来。如果我将下面我的backButtonItem的代码更改为leftBarButton它将正常工作 相信我,当我说我已经试着浏览了好几篇SO文章,即使是在我创作这篇文章的时候,我也在查找相关的文章,但我找不到任何有效的东西 这是我的UINavigationController课程: import UIKit class NavViewController: UINa

我真的很难让我所有的导航栏元素都显示在我正在创建的应用程序中。正如你所看到的,右边的按钮看起来很好,但是我的标题和后退按钮没有显示出来。如果我将下面我的
backButtonItem
的代码更改为
leftBarButton
它将正常工作

相信我,当我说我已经试着浏览了好几篇SO文章,即使是在我创作这篇文章的时候,我也在查找相关的文章,但我找不到任何有效的东西

这是我的
UINavigationController
课程:

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.
    }
}
如果你能给我任何帮助,我将不胜感激。 谢谢

编辑

因此,我继续将其添加到我的appdelegate.swift中,我可以使用后退按钮显示我想要的方式以及我的标题,但我的右键不会显示

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

}


@KyleMassacre,UINavigationController用于从一个控制器导航到另一个控制器

它自动显示后退按钮

在AppDelegation中,您只需要:

        //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)
在具有2个视图控制器的示例中。从第一个视图控制器到第二个视图控制器的按钮“按下”:

import UIKit

SecondViewController中的代码:

import UIKit
类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#>)
}
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.")
}
override func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
let rightButton=UIBarButtonim(标题:“家”,风格:UIBarButtonimStyle.Plain,目标:自我,行动:“goHome”)
rightButton.tintColor=UIColor(红色:255,绿色:255,蓝色:255,alpha:1)
navigationItem.rightBarButtonItem=rightButton
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
/*
//标记:-导航
//在基于故事板的应用程序中,您通常需要在导航之前做一些准备
覆盖函数prepareforsgue(segue:UIStoryboardSegue,sender:AnyObject?){
//使用segue.destinationViewController获取新的视图控制器。
//将选定对象传递给新的视图控制器。
}
*/
func goHome(){
//导航到您想要的位置。您可以使用:
//navigationController?.pushViewController(,动画:)
//导航控制器?.popToRootViewControllerAnimated(动画:Bool)
//presentViewController(,动画:,完成:Void)?##()->Void#>)
}
}

结果是:


@KyleMassacre,UINavigationController用于从一个控制器导航到另一个控制器

它自动显示后退按钮

在AppDelegation中,您只需要:

        //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)
在具有2个视图控制器的示例中。从第一个视图控制器到第二个视图控制器的按钮“按下”:

import UIKit

SecondViewController中的代码:

import UIKit
类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#>)
}
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.")
}
override func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
let rightButton=UIBarButtonim(标题:“家”,风格:UIBarButtonimStyle.Plain,目标:自我,行动:“goHome”)
rightButton.tintColor=UIColor(红色:255,绿色:255,蓝色:255,alpha:1)
navigationItem.rightBarButtonItem=rightButton
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
/*
//标记:-导航
//在基于故事板的应用程序中,您通常需要在导航之前做一些准备
覆盖函数prepareforsgue(segue:UIStoryboardSegue,sender:AnyObject?){
//使用segue.destinationViewController获取新的视图控制器。
//将选定对象传递给新的视图控制器。
}
*/
func goHome(){
//导航到您想要的位置。您可以使用:
//navigationController?.pushViewController(,动画:)
//导航控制器?.popToRootViewControllerAnimated(动画:Bool)
//presentViewController(,动画:,完成:Void)?##()->Void#>)
}
}

结果是:


@KyleMassacre,UINavigationController用于从一个控制器导航到另一个控制器

它自动显示后退按钮

在AppDelegation中,您只需要:

        //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)
在具有2个视图控制器的示例中。从第一个视图控制器到第二个视图控制器的按钮“按下”:

import UIKit

SecondViewController中的代码:

import UIKit
类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#>)
}
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.")
}
override func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
let rightButton=UIBarButtonim(标题:“家”,风格:UIBarButtonimStyle.Plain,目标:自我,行动:“goHome”)
rightButton.tintColor=UIColor(红色:255,绿色:255,蓝色:255,alpha:1)
navigationItem.rightBarButtonItem=rightButton
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
/*
//标记:-导航
//在基于故事板的应用程序中,您通常需要在导航之前做一些准备
覆盖函数prepareforsgue(segue:UIStoryboardSegue,sender:AnyObject?){
//使用segue.destinationViewController获取新的视图控制器。
//将选定对象传递给新的视图控制器。
}
*/
func goHome(){
//导航到您想要的位置。您可以使用:
//navigationController?.pushViewController(,动画:)
//导航控制器?.popToRootViewControllerAnimated(动画:Bool)
//presentViewController(,动画:,完成:Void)?##()->Void#>)
}
}

结果是:


@KyleMassacre,UINavigationController用于从一个控制器导航到另一个控制器

它自动显示后退按钮

在AppDelegation中,您只需要:

        //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)
在具有2个视图控制器的示例中。按钮“推”从第一视图控制器到第二视图控制器