2

I have 2 ViewControllers that are presenting a new ViewController.

The first is in a navigation controller so it works as expected with a segue push.

The second however is from a ViewController without a navigation bar. I'm programmatically presenting this view. However when the destination is presented there are 2 issues...

1) There is no navigation bar. 2) The view shown starts below the first TableViewCell.

func goToLocation() {
    let locationTableVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "locationProfile") as! LocationTableViewController
    locationTableVC.documentId = selectedDocumentId!
    self.present(locationTableVC, animated: true, completion: nil)
}

LocationTableViewController.swift

// MARK: - View Will Appear
override public func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    UIApplication.shared.statusBarStyle = .lightContent

    // Make Nav Bar Translucent and Set title font/color
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.view.backgroundColor = .clear
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: .semibold)]
    self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "back-arrow-white")

}

enter image description here

Segue shows starting below the first TableViewCell and without a navigation bar. Segue shows starting below the first TableViewCell and without a navigation bar.

The first segue which I'm trying to recreate like the second looks like this... enter image description here

pjmanning
  • 1,241
  • 1
  • 20
  • 48
  • "1)" is easy, it doesn't have a navigation controller, because you don't have one from that view. You must push the view controller embedded in a UINavigationController to have one. It may also solve "2)" if it works when pushing from the other view. – Daniel Dec 18 '17 at 18:47
  • @Daniel So if i didn't want to the navigation bar shown on the first VC though i'd just hide it? – pjmanning Dec 18 '17 at 18:49
  • You don't need to hide it, just create one when you need it. See my answer. – Daniel Dec 18 '17 at 18:52

1 Answers1

1

Push your UIViewController with a UINavigationController, like this:

func goToLocation() {
    let locationTableVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "locationProfile") as! LocationTableViewController
    locationTableVC.documentId = selectedDocumentId!
    let navigationController = UINavigationController(rootViewController: locationTableVC)
    self.present(navigationController, animated: true, completion: nil)
}
Daniel
  • 20,420
  • 10
  • 92
  • 149
  • Thanks @Daniel that did solve #2 as well. The only thing I need to do is set the back/dismiss button up as well. Do I add this in the `goToLocation` function? Or would this be in the `locationTableVC`? – pjmanning Dec 18 '17 at 19:05
  • It would be in the locationTableVC – Daniel Dec 18 '17 at 20:57
  • This seems to override the other back button. Because then I have to call a function and `popViewController` which works for the other view but for the ViewController coming from `goToLocation` – pjmanning Dec 18 '17 at 21:40
  • Thats right, you need an if to decide if you need to add that button or not – Daniel Dec 18 '17 at 22:06
  • 100% otherwise there's no way to leave the VC – pjmanning Dec 18 '17 at 22:09