不在导航栏上制作栏按钮项目和标题(iOS,Swift)
Posted
技术标签:
【中文标题】不在导航栏上制作栏按钮项目和标题(iOS,Swift)【英文标题】:not making bar button items and title on nav bar (iOS, Swift) 【发布时间】:2016-01-10 11:33:26 【问题描述】:在我的主导航控制器上,有一个名为"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
,它是 Main Storyboard 中 customVC
的一种类型。 customVC
是 UIViewController 的子类。然后,用CustomLoginVC
初始化 UINavigationController 并分配左右栏按钮项。但是,标题和栏按钮项目未显示在导航栏上,如下图所示。
我不明白为什么导航栏上没有制作栏按钮项目和标题。
同样在AppDelegate
,我设置的方法如下:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
UINavigationBar.appearance().barTintColor = UIColor(hex: "00BFA5")
application.statusBarStyle = UIStatusBarStyle.LightContent
return true
感谢任何cmets!
【问题讨论】:
【参考方案1】:您需要在CustomLoginVC
而不是navVC
上设置标题和按钮
【讨论】:
当我按照你告诉我的操作时,它起作用了。但我不明白为什么我需要在 CustomLoginVC 上而不是在 navVC 上设置标题和按钮。我不需要在 UINavigationController 上设置按钮和标题吗? CustomLoginVC 不是 UINavigationController 而是 UIViewController 我们总是在导航控制器的关联视图控制器上设置按钮和标题。甚至相应 barbuttons 的操作方法也是在 viewcontroller 中编写的,而不是在导航控制器中编写的。导航控制器只是视图控制器的持有者,可以在其中推送和弹出【参考方案2】:我认为你应该写到下面三行试试:
navVC.navigationItem.leftBarButtonItem = nil;
navVC.navigationItem.rightBarButtonItem = nil;
navVC.navigationItem.hidesBackButton = true;
然后尝试写:
navVC.navigationItem.setLeftBarButtonItem(backButton, animated: false)
navVC.navigationItem.setRightBarButtonItem(exportButton, animated: false)
或
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
【讨论】:
以上是关于不在导航栏上制作栏按钮项目和标题(iOS,Swift)的主要内容,如果未能解决你的问题,请参考以下文章
向导航栏添加渐变将隐藏 iOS 13.0 + 中的栏按钮项目