以编程方式创建导航视图

时间:2016-12-15 20:06:41

标签: swift uiviewcontroller uinavigationcontroller storyboard uinavigationbar

所以我试图创建一个应用程序,但我试图避免使用故事板。因此,只需使用swift文件和XIB文件。

之前我曾经使用过导航控制器,但我认为还不够。到目前为止,我有这个:

在AppDelegate中我有:

let homeVC = HomeViewController()
let rootVC = UINavigationController(rootViewController: homeVC)

window!.rootViewController = rootVC
window!.makeKeyAndVisible()

我的观点目前完全是空的,但基本上是" View"创建新XIB文件时出现的屏幕。我已将其大小设置为freeform,其他所有内容(如顶栏,状态栏)均为Inferred

在我的HomeViewController.swift中,我有:

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    let nib = UINib(nibName: "HomeView", bundle: nil)
    let objects = nib.instantiateWithOwner(self, options: nil)
    self.view = objects[0] as! UIView;

    print(self.navigationController)

    // customize navigation bar
    let settingsImage = UIImage(named: "settingsWheelBlack.png")
    let settingsNavItem = UIBarButtonItem(image: settingsImage, style: UIBarButtonItemStyle.Plain, target: nil, action: Selector("selector"))
    let addStuffItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: nil, action: Selector("selector"))

    self.navigationController?.navigationItem.title = "Home"
    self.navigationController?.navigationItem.leftBarButtonItem = settingsNavItem
    self.navigationController?.navigationItem.rightBarButtonItem = addStuffItem

    print(self.navigationController?.navigationBar)
    print(self.navigationController?.navigationItem.title)
}

但是当我运行应用程序时,导航栏不会显示。除了我现在所拥有的以外,这就是我所尝试的:

  1. 将导航栏控件添加到我的XIB并将IB插座连接到它。同时将IB插座连接到导航栏控件中已存在的导航项。然后设置标题,左右按钮。没有工作

    1. 立即在AppDelegate中为上面定义的rootVC设置标题和按钮。没有工作。
  2. 我缺少什么想法?

1 个答案:

答案 0 :(得分:0)

我在收集了一些力量来阅读大量的Apple文档后解决了这个问题。在这个页面上,我发现了这一小段文字:

In a navigation interface, each content view controller in the navigation stack provides a navigation item as the value of its **navigationItem** property. The navigation stack and the navigation item stack are always parallel: for each content view controller on the navigation stack, its navigation item is in the same position in the navigation item stack

所以我按原样离开了我的AppDelegate代码,并将viewDidLoad函数更改为:

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    let nib = UINib(nibName: "EventsHomeView", bundle: nil)
    let objects = nib.instantiateWithOwner(self, options: nil)
    self.view = objects[0] as! UIView;

    print(self.navigationController)

    // customize navigation bar
    let settingsImage = UIImage(named: "settingsWheelBlack.png")
    let settingsNavItem = UIBarButtonItem(image: settingsImage, style: UIBarButtonItemStyle.Plain, target: nil, action: Selector("selector"))
    let addStuffItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: nil, action: Selector("selector"))

    // Each VC within a navigation controller has it's own navigationItem property that the underlying navigation controller uses to show in the navigationBar
    self.navigationItem.title = "Home"
    self.navigationItem.leftBarButtonItem = settingsNavItem
    self.navigationItem.rightBarButtonItem = addStuffItem
}

中提琴!