不在导航栏上制作栏按钮项目和标题(iOS,Swift)

时间:2016-01-10 11:33:27

标签: ios swift uinavigationcontroller

在我的主导航控制器上,有一个名为"Next Page"的右侧栏按钮。按下按钮时,它会调用名为"nextPage()"的方法,该方法初始化导航控制器并显示它。

nextPage()的代码如下:

func nextPage() {
        let window = UIWindow(frame: UIScreen.mainScreen().bounds)
        let CustomLoginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("viewControllerID") as! customVC

        let navVC = UINavigationController.init(rootViewController: CustomLoginVC)
        navVC.view.autoresizingMask = [.FlexibleHeight, .FlexibleWidth, .FlexibleTopMargin, .FlexibleLeftMargin]

        navVC.navigationBar.translucent = false
        navVC.navigationItem.title = "haha"
        navVC.navigationBar.tintColor = UIColor.whiteColor()
        navVC.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "AvenirNext-Medium", size: 22)!, NSForegroundColorAttributeName: UIColor.whiteColor() ]
        navVC.navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "AvenirNext", size: 18)!], forState: UIControlState.Normal)

        let backButton = UIBarButtonItem.init(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "backButtonPressedInPDF")
        let exportButton = UIBarButtonItem.init(image: UIImage(named: "export"), style: UIBarButtonItemStyle.Plain, target: self, action: "saveAsPDF")

        navVC.navigationItem.setLeftBarButtonItem(backButton, animated: false)
        navVC.navigationItem.setRightBarButtonItem(exportButton, animated: false)
        window.rootViewController = navVC
        self.presentViewController(navVC, animated: true, completion: nil)
    }

正如您所看到的,我正在实例化CustomLoginVC,这是一种来自主故事板的customVCcustomVC是UIViewController的子类。然后,使用CustomLoginVC初始化UINavigationController并分配左右栏按钮项。但是,标题和条形按钮项目不会显示在导航栏上,如下图所示。

enter image description here

我不明白为什么不在导航栏上创建栏按钮项目和标题。

同样在AppDelegate中,我将方法设置如下:

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        UINavigationBar.appearance().barTintColor = UIColor(hex: "00BFA5")
        application.statusBarStyle = UIStatusBarStyle.LightContent

        return true
    }

感谢任何评论!

2 个答案:

答案 0 :(得分:1)

您需要在CustomLoginVC而非navVC

上设置标题和按钮

答案 1 :(得分:0)

我认为你应该写下面的三行试试吧:

    navVC.navigationItem.leftBarButtonItem = nil;
    navVC.navigationItem.rightBarButtonItem = nil;
    navVC.navigationItem.hidesBackButton = true;

之后尝试写:

navVC.navigationItem.setLeftBarButtonItem(backButton, animated: false)
navVC.navigationItem.setRightBarButtonItem(exportButton, animated: false)

OR

self.title = "Your Title"

var homeButton : UIBarButtonItem = UIBarButtonItem(title: "LeftButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: "")

var logButton : UIBarButtonItem = UIBarButtonItem(title: "RigthButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: "")

self.navigationItem.leftBarButtonItem = homeButton
self.navigationItem.rightBarButtonItem = logButton