首页 > 解决方案 > 自定义 NavigationBarButton 在 iOS 12 及更低版本上没有控制器,iOS 13 正在运行

问题描述

我正在尝试使用带有自己的 ViewController 的 CustomView 作为正确的 NavigationBar(barbuttonitem)。此视图需要根据应用状态进行更新。视图是这样实现的:

private func addUploadStatusToNavigationController() {        
        cloudView = UploadStatusViewController<UploadStatusNavigationView>()

        let tgr = UITapGestureRecognizer(target: self, action: #selector(cloudViewTapped))
        cloudView.customView.addGestureRecognizer(tgr)

        let cloudViewButton = UIBarButtonItem(customView: cloudView.customView)
        self.navigationItem.rightBarButtonItem = cloudViewButton

        cloudView.setStatus(status: uploadStatusService.getStatus())

        if let btn = self.navigationItem.rightBarButtonItem?.customView {
            btn.frame = CGRect(x: 0, y: 0, width: (self.navigationController?.navigationBar.frame.width ?? 0) * 0.6, height: 44)
        }

        uploadStatusService.delegate = self
    }

此方法在 viewWillAppear 中调用。

在我通过点击视图选项卡打开警报之前,iOS 13 一切正常。如果我再次关闭它,视图会丢失它自己的控制器 - UploadStatusViewController -(至少在 ViewDebugger 中)。通过此控制器运行的状态更新仍然可以正常工作。水龙头不再被识别。

在 iOS 12 中,没有检测到轻按,甚至第一次也没检测到。

显示警报如下所示:

private func askUserToStop() {
        let alert = UIAlertController(title: "OneDrive", message: "BackupPopup.Cancel.Message".localized, preferredStyle: .alert)
        let action1 = UIAlertAction(title: "Button_Title_Cancel".localized, style: .cancel, handler: nil)
        alert.addAction(action1)
        self.present(alert, animated: true, completion: nil)
    }

如果我将一个新控制器推到 NavigationStack 上并再次导航回来,则 GestureRecognizer 只能再次工作(iOS13)。我实际上希望 TapGestureRecognizer 能够在弹出窗口出现之后保留下来,并且也可以在 iOS 12 下工作。

恐怕我不知道还能尝试什么。

编辑:

总结:

标签: iosswiftcontrolleruikitnavigationbar

解决方案


推荐阅读