我正在努力让所有导航栏元素显示在我正在创建的应用中。你可以看到右边的按钮显示正常,但我的标题和我的后退按钮没有显示。如果我将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)
}
}
答案 0 :(得分:2)
根据您的问题,似乎您不希望在单个调用中设置左右按钮,并且您需要通过编写一次来获取它们并将它们放入所有其他类中。这是你需要做的。
在AppDelegate.swift中
func application(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - >布尔{
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
UINavigationBar.appearance().barTintColor = UIColor(red: 0.0627, green: 0.4862, blue: 0.0627, alpha: 1)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
return true
}
以上代码将在整个应用程序中为导航栏设置此属性。
现在,添加新文件并将其命名为RootViewController - “UIViewController的子类”。
导入UIKit
类RootViewController:UIViewController {
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.")
}
现在在你所有的View Controller中你只需要“替换 - 用RootViewController替换UIViewController”。
现在在Second View Controller中你需要做的就是从SecondViewController的ViewDidLoad中的RootViewController调用方法。
覆盖func viewDidLoad(){
super.viewDidLoad()
self.navigationItem.title =“第二视图”
setLeftBarButton()
setRightBarButton()
//加载视图后进行任何其他设置
}
输出:
希望这可以解决您的问题。
答案 1 :(得分:1)
@KyleMassacre,UINavigationController用于从一个控制器导航到另一个控制器UINavigationController Apple Doc
自动显示后退按钮。
在您的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个视图控制器的示例中。按钮“推”从第一个View Controller到SecondViewController:
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#>)
}
}
结果: