在视图和显示按钮内添加UINavigationBar

时间:2017-01-11 07:18:55

标签: ios

我有viewController。我的内部是UIView

lazy var cView : UIView = {
    var view = UIView()
    view.backgroundColor = .red
    return view
}()

在此视图中,我有UINavigationBar

lazy var navigationBar : UINavigationBar = {
    let bar = UINavigationBar()
    let leftButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.done, target: self, action: #selector(cancel))
    bar.topItem?.setLeftBarButton(leftButton, animated: true)
    let rightButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(done))
    bar.topItem?.setRightBarButton(rightButton, animated: true)
    bar.barTintColor = .green
    bar.tintColor = UIColor.red
    bar.barStyle = .default
    bar.isTranslucent = false
    return bar
}()

问题是在viewController中我只看到带有绿色条并没有任何按钮的红色视图。

我做错了什么?如何使按钮可见?

1 个答案:

答案 0 :(得分:0)

bar.topItem初始化UINavigationBar时,UINavigationItem为nil。你可以使用bar.setItems(items:[UINavigationItem]?,animated:bool)方法来设置bar.topItem