UINavigationBar无法正确加载

时间:2018-09-01 18:09:25

标签: swift uinavigationbar uibarbuttonitem uinavigationitem

我知道关于这个话题有很多疑问;但是,这些答案都没有帮助我,我尝试了许多解决此问题的方法。我的问题是,原来没有显示我的条形按钮,但是稍后在应用程序中显示ViewController时,它会显示出来,但是导航标题不会显示。我不确定为什么会这样,但是我认为这与我使用的SwipeNavigationController框架有关。我的目标是使按钮按预期显示,即用户向左滑动以进入该视图时,以及稍后调用该视图并使其呈现为相同外观时。添加navItem的代码如下:

let cameraBarButton = UIBarButtonItem(image: #imageLiteral(resourceName: "cameraIcon"), style: .plain, target: self, action: #selector(goToCamera))
navigationItem.rightBarButtonItem = cameraBarButton

请查看其他post,以更好地了解该框架。以下是有关如何设置导航栏的代码:

 override func viewDidLoad() {
    super.viewDidLoad()

    setupView()

    let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
    let displayWidth: CGFloat = self.view.frame.width
    let displayHeight: CGFloat = self.view.frame.height

    messagesTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
    messagesTableView.register(BlankCell.self, forCellReuseIdentifier: blankCellID)
    messagesTableView.dataSource = self
    messagesTableView.delegate = self
    self.view.addSubview(messagesTableView)

    view.backgroundColor = UIColor.white

    setupNavButtons()
    setupNavBar()

   showNoMessagesLabel()



    navigationController?.isNavigationBarHidden = false

    if #available(iOS 11.0, *) {
        navigationController?.navigationBar.prefersLargeTitles = true
        self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    } else {
        // Fallback on earlier versions
    }
}
override func viewWillAppear(_ animated: Bool) {



    if #available(iOS 11.0, *) {
        navigationController?.navigationBar.prefersLargeTitles = true
        self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        setupNavBar()
    } else {
        setupNavBar()
    }
}

func setupNavBar() {



    UIApplication.shared.statusBarStyle = .lightContent
    self.navigationController?.isNavigationBarHidden = false
    self.navigationController?.navigationBar.topItem?.title = "Messages"
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    self.navigationController?.navigationBar.barTintColor = UIColor.pinkNeonColor
    self.navigationController?.navigationBar.tintColor = UIColor.white


}

func setupNavButtons() {
    let cameraButton = UIButton(type: .system)
    cameraButton.setImage(#imageLiteral(resourceName: "cameraIcon").withRenderingMode(.alwaysOriginal), for: .normal)
    cameraButton.frame = CGRect(x: 0, y: 0, width: 34, height: 34)
    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: cameraButton)
}

0 个答案:

没有答案