导航栏不可见

问题描述 投票:0回答:1

我正在尝试设置 navigationBar 在xcode中用swift 4编程。我也希望带标题的后退按钮也能出现。

在我的逻辑之后,我的应用程序上似乎没有出现任何东西。我不知道什么是不正确的。请大家帮忙。

import UIKit

class SellBaseViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

       self.setNavigationBar()
           setLayout()

    }

    func reload() {
        self.viewDidLoad()
    }
    func setNavigationBar() {
        let width = self.view.frame.width
        let navigationBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: width, height: 44))
        self.view.addSubview(navigationBar);
        let navigationItem = UINavigationItem(title: "Navigation bar")
        let doneBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: nil, action: #selector(selectorX))
        navigationItem.rightBarButtonItem = doneBtn
        navigationBar.setItems([navigationItem], animated: false)
    }

    func setLayout(){

    }
   @objc func selectorX() {

    }
}
swift xcode uinavigationbar
1个回答
0
投票

试着把你的代码放在viewWillAppear中。

override func viewWillAppear(_ animated: Bool) {

        super.viewWillAppear(animated)
        self.setNavigationBar()
           setLayout()
© www.soinside.com 2019 - 2024. All rights reserved.