【问题标题】:full screen background image while using uinavigationcontroller使用 uinavigationcontroller 时全屏背景图像
【发布时间】:2015-03-30 20:46:44
【问题描述】:

我有一个 UINavigationController,其中 UINavigationBar hidden = YES。

我想要嵌入在 UINavigationController 中的全屏视图。

但我得到的只是: http://cs616618.vk.me/v616618797/1bf0d/FEdIn0Nn4x8.jpg

可以在状态栏下全屏吗? 我通过独立的视图控制器实现了这一点,但是在 UINavigationController 中使用它时,它就像在图像上一样。

【问题讨论】:

标签: ios ios7 uinavigationcontroller uinavigationbar uistatusbar


【解决方案1】:

检查您的所有视图控制器是否已正确配置:

UINavigationController:

rootViewController(在UINavigationController内):

这是我使用上述配置和UIImageView 上的“纵横比填充”设置得到的结果:

如果您想以编程方式执行此操作,请尝试以下操作:

在您的视图控制器的代码中:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

以及您在哪里初始化 UINavigationController(AppDelegate 等):

navController.edgesForExtendedLayout = UIRectEdgeAll;

当然,不要忘记注释或删除所有可能干扰这些设置的代码行。

【讨论】:

  • 不适合我,仍在导航栏下方
【解决方案2】:

这是我使用 Swift 5、XCode 12 为我所做的工作。

第 1 步(可选) - 创建自定义 UINavigationController 类

class CustomNavigationController: UINavigationController {
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationBar.isTranslucent = true
}

用这个 UINavigationController 子类替换你的 UINavigationController。我将此标记为可选,因为这是基于偏好的,如果您不设置此选项,您的导航栏将是不透明的,并且您看不到它下面的内容。

设置navigationBar.isTranslucent = true 可以让你看到它下面的背景,这是我喜欢的。子类也是可选的,但您可能需要对导航栏进行其他更新,所以我总是喜欢将其设为子类。

第 2 步 - 设置背景视图约束

class CustomViewController: UIViewController {
  // your background view
  let bgImageView: UIImageView = {
    let bgImageView = UIImageView()
    bgImageView.image = UIImage(named: "gradient_background")
    bgImageView.contentMode = .scaleAspectFill
    return bgImageView
}()

  // Get the height of the nav bar and the status bar so you
  // know how far up your background needs to go
  var topBarHeight: CGFloat {
    var top = self.navigationController?.navigationBar.frame.height ?? 0.0
    if #available(iOS 13.0, *) {
      top += UIApplication.shared.windows.first?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
    } else {
      top += UIApplication.shared.statusBarFrame.height
    }
    return top
  }

  var isLayoutConfigured = false

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    title = "Site Visit"

    // you only want to do this once
    if !isLayoutConfigured() {
      isLayoutConfigured = true
      configBackground()
    }
  }

  private func configBackground() {
    view.addSubview(bgImageView)
    configureBackgroundConstraints()
  }
  
  // Set up your constraints, main one here is the top constraint
  private func configureBackgroundConstraints() {
    bgImageView.translatesAutoresizingMaskIntoConstraints = false
    bgImageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor,
                                     constant: -topBarHeight).isActive = true
    bgImageView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor,
                                         constant: 0).isActive = true
    bgImageView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,
                                        constant: 0).isActive = true
    bgImageView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor,
                                          constant: 0).isActive = true
    
    view.layoutIfNeeded()
}

在设置约束之前:

设置上述约束后:

【讨论】:

    猜你喜欢
    • 2016-12-31
    • 2011-01-13
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多