Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 13 - Right Bar Button Item goes offset in Modal Sheet presentation

Tags:

xcode

swift

ios13

I have this strange problem with iOS 13 and its new sheet cards style modal presentation.

From ViewController1 I modal present ViewController2 embedded in a NavigationController, and everything works fine.
From ViewController2, I then modal present ViewController3 embedded in a NavigationController, and I get the Right Bar Button offset.

Here is a video of the problem: does anybody have a fix?

What am I doing wrong?

Main View Controller

import UIKit

let vc1identifier = "vc1identifier"
let vc2identifier = "vc2identifier"

class ViewController: UIViewController {

@IBAction func tap1(_ sender: UIButton) {
    if let navigation = self.storyboard?.instantiateViewController(withIdentifier: vc1identifier) as? UINavigationController,
        let nextVC = navigation.contentViewController as? UnoViewController {

        //self.navigationController?.pushViewController(nextVC, animated: true)
        self.present(navigation, animated: true, completion: nil)
    }
}
}

extension UIViewController {
var contentViewController: UIViewController {
    if let navcon = self as? UINavigationController {
        return navcon.visibleViewController!
    } else {
        return self
    }
}
}

Second View Controller

import UIKit

class UnoViewController: UIViewController {

@IBOutlet weak var barButton: UIBarButtonItem!


override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    barButton.title = "GotoVC2"
}

@IBAction func pressThis(_ sender: UIBarButtonItem) {
    if let navigation = self.storyboard?.instantiateViewController(withIdentifier: vc2identifier) as? UINavigationController,
        let nextVC = navigation.contentViewController as? DueViewController {

        self.present(navigation, animated: true, completion: nil)
    }
}
}
like image 855
Pepo Avatar asked Oct 04 '19 08:10

Pepo


1 Answers

I came across the same issue.

Solution is easy, you just need to tell navigationbar it needs layout like this

 override public func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if #available(iOS 13.0, *) {
       navigationController?.navigationBar.setNeedsLayout()
    }
 }
like image 133
Angelus Avatar answered Oct 15 '22 02:10

Angelus