首页 > 解决方案 > 如何更新 Swift5 + Xcode11.6 的工具栏?

问题描述

Swift3 应用程序之前(1-2 年前)运行良好。现在它停止在底部显示工具栏按钮。

有 2 个输入:程序化和情节提要。他们都没有出现。以编程方式(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()
}

有一个导航控制器(初始视图)。VideoViewController 是由以前的 VC 调用的:

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

此 VC 当前状态的屏幕截图:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

VideoViewController 启动时的截图:

在此处输入图像描述

标签: swifttoolbarswift5xcode11

解决方案


这个解决方案Swift3对我有用:

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

问题是 - 根UINavigationController。以前在 Apple 添加 SceneDelegate 之前它很好,因为我必须将 设置UINavigationController为 Storyboard 的初始视图。就是这样。现在我们可以工作了。

现在,我们有了 SceneDelegate(我已经删除了这个文件)。AppDelegate 现在应该引用这个导航控制器(确保它在故事板上有标识符,就像我们为视图控制器所做的那样)。AppDelegate 文件现在应该使用 Navigation Controller:

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
}

推荐阅读