我尝试以编程方式使用按钮和标题制作导航栏。由于某种原因,导航栏显示,但按钮和标题没有显示。你如何解决这个问题?
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, 500, 200))
//Create navigation bar's title
self.navigationController?.navigationBar.topItem?.title = "Edit"
//Create back button
let backBtn = UIButton(type: UIButtonType.Custom)
let backButtonItem = UIBarButtonItem(customView: backBtn)
self.navigationItem.leftBarButtonItem = backButtonItem
//Add navigation bar to view
self.view.addSubview(navigationBar)
最佳答案
对于你的问题,由于你没有将 View Controller 嵌入到导航 Controller 中,所以你的所有设置都没有意义
self.navigationController? = nil &
self.navigationItem.leftBarButtonItem //there's no navigationcontroller made no sense
你需要做的是,在适当的时候将vc嵌入到navigationcontroller
let navVC = UINavigationController(rootViewController: vc)
然后你可以使用 navigationController 并设置所有内容
关于swift - 当我以编程方式添加按钮和标题时,为什么按钮和标题没有显示在导航栏上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34562599/