Search code examples
swifttoolbarswift5xcode11

How to update the toolbar for Swift5 + Xcode11.6?


Swift3 application worked fine before (1-2 years ago). Now it stopped to show the toolbar button at the bottom.

There are 2 inputs: programmatically and storyboarded. None of them shows up. Programatically(VideoViewController):

override func viewDidLoad()
{
    super.viewDidLoad()
    
    // nabigationBar customization
    self.navigationController?.isToolbarHidden = false
    //self.navigationController?.setToolbarHidden(false, animated: true)
    var items = [UIBarButtonItem]()
    items.append( UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) )
    items.append( UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: #selector(buttonCapture(_:))) )
    items.append( UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) )
    self.toolbarItems = items
    
    //captureSession to capture the current image
    captureSession.sessionPreset = .photo
    guard let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .front) else { return }
    guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return }
    captureSession.addInput(input)
    captureSession.startRunning()
    
    let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    view.layer.addSublayer(previewLayer)
    previewLayer.frame = view.frame
    
    let dataOutput = AVCaptureVideoDataOutput()
    dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
    captureSession.addOutput(dataOutput)
    setupIdentifierConfidenceLabel()
}

There is a navigation controller (initial view). The VideoViewController is being called by this from previous VC:

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let videoVC = storyBoard.instantiateViewController(withIdentifier: "VideoViewController") as UIViewController
self.present(videoVC, animated: true, completion: nil)

Screenshots of the current status for this VC:

enter image description here

enter image description here

enter image description here

Screenshot during the launch of the VideoViewController:

enter image description here


Solution

  • This solution Swift3 worked for me:

    let newViewController = NewViewController()
    self.navigationController?.pushViewController(newViewController, animated: true)
    

    The problem was - root UINavigationController. Previously before Apple added SceneDelegate it was fine, because I had to set the UINavigationController as the initial view at the Storyboard. That's it. Now we can work.

    Now, we have SceneDelegate (I've deleted this file). AppDelegate should refer now to this Navigation Controller (make sure it has identifier at storyboard as we do for View Controller to segue). AppDelegate file should use Navigation Controller now:

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "navigator") as! UINavigationController
        
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = initialViewController
        self.window?.makeKeyAndVisible()
        
        return true
    }