【问题标题】:How to present modalViewController over all VC's in TabBarController如何在 TabBarController 中的所有 VC 上呈现 modalViewController
【发布时间】:2023-03-20 07:15:01
【问题描述】:

我有一个带有 5 个选项卡的 UITabBarController。我希望能够在所有选项卡上显示一个 ViewController,就在选项卡栏的上方。目前我遇到的问题是,当我以模态方式呈现 ViewController 时,它是不可见的。每当我在 tableView 中选择一行时,都会调用此函数:

调用showPlayerView的tableView定义如下:

class SearchResultsTableView: UIView, UITableViewDataSource, UITableViewDelegate

searchResultsTableView 类是另一个类中的视图,定义如下:

TestViewController: UIViewController, UserSearchDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource, UINavigationBarDelegate, UINavigationControllerDelegate

这是实际的功能:

func showPlayerView() {
    let playerView = PlayerView()
    playerView.modalPresentationStyle = .overCurrentContext
    CustomTabBarController.sharedInstance?.present(playerView, animated: true, completion: {

    })
}

然后我像这样设置 CustomTabBarController sharedInstance:

static var sharedInstance : CustomTabBarController?

viewDidLoad() 里面我有这个:

CustomTabBarController.sharedInstance = self

为什么我看不到我的模态 ViewController,我怎样才能让它显示在所有屏幕上?

【问题讨论】:

  • 您能否提供更多有关您在何处运行 showPlayerView 方法的背景信息?
  • 刚刚编辑了问题并添加了正在运行的类
  • 或许可以使用导航控制器?
  • 但我需要它显示在我的 TabBarController 中的所有 5 个 VC 中。使用导航栏可以做到这一点吗?
  • 显示更多代码和描述以便更好地理解以帮助您。

标签: ios swift uitabbarcontroller uimodalpresentationstyle


【解决方案1】:
func showPlayerView() {
    let playerView = PlayerView()
    playerView.modalPresentationStyle = .overCurrentContext
    CustomTabBarController.sharedInstance?.tabBarController?.tabBar.hidden = true  // This will hide tab bar of your UITabbarViewController.
    CustomTabBarController.sharedInstance?.present(playerView, animated: true, completion: {

    })
}

// Make it visible in ViewWillApear method by below line

CustomTabBarController.sharedInstance?.tabBarController?.tabBar.hidden = false

【讨论】:

  • 那行不通。首先,我不能调用self.present,因为self 指的是UIView。当我将 self.present 更改为 CustomTabBarController.sharedInstance?.present 以尝试解决此问题时,出现此错误:Warning: Attempt to present <PlayerView: 0x7fa750911600> on <CustomTabBarController: 0x7fa74e81d600> whose view is not in the window hierarchy!
  • 我已经更新了我的答案。再试一次。希望对您有所帮助。
  • 来自 TestViewController 将删除警告。您可以从 SearchResultsTableView 中设置对 TestViewController 的回调
  • 但是它不会出现在UITabBar中的所有5个viewController上
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 1970-01-01
相关资源
最近更新 更多