4

NOTE: This is not about the new default modal presentation style used in iOS 13.

I have a strange issue presenting a modal UINavigationController.

Consider an UIViewController that is inside an UINavigationController:

enter image description here

When this code runs on iOS 13.0 :

@IBAction func btntap(_ sender: Any) {

    let errorViewController = UIViewController()
    errorViewController.view.backgroundColor = .blue
    errorViewController.title = "Erro na solicitação"

    let errorNavigation = UINavigationController()

    errorNavigation.navigationBar.barTintColor = UIColor(red: 204/255, green: 0/255, blue: 0/255, alpha: 1.0)

    errorNavigation.navigationBar.tintColor = UIColor.white
    errorNavigation.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]

    errorNavigation.setViewControllers([errorViewController], animated: false)

    errorNavigation.modalPresentationStyle = .automatic

    self.present(errorNavigation, animated: true, completion: nil)
 }

This happens:

enter image description here

Note the wrong height when we present the modal screen on the first time:

enter image description here

I want to continue using the card-like presentation, but I need to fix this wrong height issue on the first present.

This happens when the following requirements are met:

  1. The presenting UIViewController is inside an UINavigationController

  2. The presented UIViewController has special chars on its title ("ç", "ã", etc)

  3. The present is animated true

Already tried some variations of layoutIfNeeded() but none worked.

How can I present this with the right height on the first present?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
alxlives
  • 5,084
  • 4
  • 28
  • 50
  • Possible duplicate of [Presenting modal in iOS 13 fullscreen](https://stackoverflow.com/questions/56435510/presenting-modal-in-ios-13-fullscreen) – EmilioPelaez Oct 28 '19 at 18:22
  • Sorry, I searched a lot and didn't found any similar issue. The problem is the navigation bar height on the first present, not the presentation form. If the title is misleading, could you suggest a better one so I can edit the question? – alxlives Oct 28 '19 at 18:22
  • This probably won't help but change `let errorNavigation = UINavigationController()` to `let errorNavigation = UINavigationController(rootViewController: errorViewController)` and remove the line `errorNavigation.setViewControllers([errorViewController], animated: false)`. – rmaddy Oct 28 '19 at 18:27
  • No, same problem, but thank you for the try – alxlives Oct 28 '19 at 18:31
  • Could you try to present from `self.navigationController` instead of just `self`? I've seen problems before when presenting from child controllers. – Sulthan Oct 28 '19 at 18:35
  • @Sulthan thanks for trying, but unfortunately the same issue occurs – alxlives Oct 28 '19 at 18:39
  • This is a very well asked question, but can you explain how I can experience this bug? I pasted your code right into my project and it works fine for me. – matt Oct 29 '19 at 01:50
  • @matt I uploaded an example [here](https://github.com/alxlives/navigationBug). I'm using Xcode 11.0 and iOS 13.0, I don't know if this issue is fixed on the newer iOS versions. – alxlives Oct 29 '19 at 12:56
  • Thanks! Your github example does elicit the bug. But when I write my own example with exactly the same code, it doesn't. Now I have to try to figure out what the difference is. – matt Oct 29 '19 at 13:18
  • Okay! I figured it out. The bug is elicited only if the _source_ view controller is _also_ inside a navigation controller. I'd say this bug is completely solid; did you submit it to Apple? – matt Oct 29 '19 at 13:20
  • Not yet, I will submit it now. – alxlives Oct 29 '19 at 13:30
  • Bug reported - https://feedbackassistant.apple.com/feedback/7416163 – alxlives Oct 29 '19 at 13:38
  • Very nice work, thank you. – matt Oct 29 '19 at 14:20

1 Answers1

1

just replace the viewcontroller's title with your own label like so. this is a hack solution but will always work and you'll never have to think about it again. in fact, i never invoke the title property of viewcontrollers, i only use labels and set them as the titleView so i can control number of lines, subtitles, justification, etc.

    let errorViewController = UIViewController()
    errorViewController.view.backgroundColor = .blue

    let errorNavigation = UINavigationController()
    let label = UILabel()
    label.text = "Erro na solicitação"
    label.textColor = .white

    errorViewController.navigationItem.titleView = label

    errorNavigation.navigationBar.barTintColor = UIColor(red: 204/255, green: 0/255, blue: 0/255, alpha: 1.0)

    errorNavigation.navigationBar.tintColor = UIColor.white

    errorNavigation.setViewControllers([errorViewController], animated: false)

    errorNavigation.modalPresentationStyle = .automatic

    self.present(errorNavigation, animated: true, completion: nil)

oh yes, and on that note, the font size should be around 17-18 from medium to bold to match ios default system for title value for viewController should you wish to match the ios system defaults

Larry Pickles
  • 4,615
  • 2
  • 19
  • 36