【问题标题】:Getting the root safe area of a UIViewController?获取 UIViewController 的根安全区域?
【发布时间】:2020-06-05 04:28:20
【问题描述】:

我有一个UIViewController,它是我的UITabBarController 的一部分,并且我有一个隐藏标签栏的功能,可以在底部显示一个与下图非常相似的视图。

基本上每当我在隐藏 tabBar 后获得安全区域时,它都不会适应隐藏的标签栏。如果那里没有标签栏,我想获得实际的安全区域,这样它在不同设备上看起来都是一样的。

是否可以在没有标签栏/导航的情况下获得根安全区域插图?

【问题讨论】:

  • 很难想象您在代码中所做的事情您是否展示了新的视图控制器?
  • 这里我应该更清楚一点,它不是一个新的视图控制器,而是视图控制器中的一个 UIView,它同时弹出和隐藏标签栏

标签: ios swift xcode


【解决方案1】:

您可以在标签栏中显示,该标签栏将显示在其顶部,因此无需隐藏标签栏。

tabBarController?.present(yourViewController, animated: true, completion: nil)

【讨论】:

    【解决方案2】:

    如果您使用 AutoLayout 并希望将自定义标签栏保留在屏幕底部,同时考虑到 iPhone X 上的安全区域,您可以简单地使用 view.safeAreaLayoutGuide.bottomAnchor 将自定义视图固定到屏幕底部。

    例如:

    let myCustomTabBar = UIView()
    // ...
    view.addSubview(myCustomTabBar)
    myCustomTabBar.translatesAutoresizingMaskIntoConstraints = false
    myCustomTabBar.heightAnchor.constraint(equalToConstant: 80).isActive = true
    myCustomTabBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    myCustomTabBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    myCustomTabBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    

    如果您想知道窗口的安全区域插图(不考虑您的标签栏或导航栏),您可以访问UIWindowsafeAreaInsets 属性,或查看它的safeAreaLayoutGuide.layoutFrame

    您还可以覆盖 UIViewControllerviewSafeAreaInsetsDidChange,并根据您要查找的行为进行必要的调整。

    【讨论】:

      【解决方案3】:
      guard let rootView = UIApplication.shared.keyWindow else { return 0 }
      
              if #available(iOS 11.0, *) {
      
      //            let topInset = rootView.safeAreaInsets.top
      
                  let bottomInset = rootView.safeAreaInsets.bottom
      
      
      
              } else {
      
                  return rootView.bounds.height
      
              }
      

      https://stackoverflow.com/a/47831485/2815002

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-09
        • 2021-05-03
        • 2011-06-29
        • 2017-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多