导航按钮不显示

Navigation button doesn't show up

我正在尝试在 iOS 工具栏中显示一个按钮,理论上它非常简单并且应该可以工作:

import UIKit

class ViewController: UIViewController {    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let refresh = UIBarButtonItem(barButtonSystemItem: .Refresh, target: self, action: "refreshTapped")
        navigationItem.leftBarButtonItem = refresh
        navigationController?.toolbarHidden = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

还是不行。按钮不显示。我尝试重新启动模拟器,从头开始创建项目等。所以我想这是我的 Swift-初学者解决问题的方式的缺陷,但我根本看不到它。

转到您的 Storyboar,select View Controller,然后在 Xcode 顶部栏 select Editor -> Embed in -> Navigation Controller。

在你的代码中,当你说

navigationController?.toolbarHidden = false

如果你 CMD + 点击 navigationController?您会在变量旁边看到一条评论:"If this view controller has been pushed onto a navigation controller, return it."

所以只有当你的视图被推送到导航控制器时才有效。