因此,我试图创建一个应用程序,但我试图避免使用情节提要。因此,仅将swift文件与XIB文件一起使用。
在此之前,我曾与导航控制器一起工作过一点,但我想还不够。到目前为止,我有这个:
在AppDelegate中,我有:
let homeVC = HomeViewController()
let rootVC = UINavigationController(rootViewController: homeVC)
window!.rootViewController = rootVC
window!.makeKeyAndVisible()
我的视图当前完全是空的,但是具有创建新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)
}
但是,当我运行该应用程序时,导航栏没有显示。这是我目前尝试过的其他方法:
将导航栏控件添加到我的XIB并将IB插座连接到它。还将IB插座连接到导航栏控件中已经存在的导航项目。然后设置标题以及其中的左右按钮。没工作
立即为上面定义的
rootVC
设置AppDelegate中的标题和按钮。没用有什么想法我想念的吗?
最佳答案
在集结了阅读大量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
}
和中提琴!
关于swift - 以编程方式创建导航 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41172513/