Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用uinavigationcontroller时的全屏背景图像_Ios_Ios7_Uinavigationcontroller_Uinavigationbar_Uistatusbar - Fatal编程技术网

Ios 使用uinavigationcontroller时的全屏背景图像

Ios 使用uinavigationcontroller时的全屏背景图像,ios,ios7,uinavigationcontroller,uinavigationbar,uistatusbar,Ios,Ios7,Uinavigationcontroller,Uinavigationbar,Uistatusbar,我有一个UINavigationController,UINavigationBar hidden=YES 我想要在UINavigationController中嵌入的视图的全屏背景 但我只知道: 可以在状态栏下全屏显示吗? 我使用独立视图控制器实现了这一点,但在UINavigationController中使用它时,它与图像上的一样。检查所有视图控制器是否正确配置: UINavigationController: UINavigationController配置“> rootViewCont

我有一个UINavigationController,UINavigationBar hidden=YES

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

但我只知道:

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

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

UINavigationController

UINavigationController配置“>

rootViewController
(在
UINavigationController
内部):

UIViewController配置“>

下面是使用上述配置和
UIImageView
上的“方面填充”设置得到的结果:

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

在视图控制器的代码中:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
以及初始化
UINavigationController
(AppDelegate等)的位置:


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

以下是我使用Swift 5,XCode 12所做的工作。

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

class CustomNavigationController: UINavigationController {
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationBar.isTranslucent = true
}
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()
}
用此UINavigationController子类替换UINavigationController。我将其标记为可选,因为这是基于首选项的,如果您不设置此选项,您的导航栏将是不透明的,并且您无法看到它下面的内容

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

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

class CustomNavigationController: UINavigationController {
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationBar.isTranslucent = true
}
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()
}
在设置约束之前:

设置上述约束后:

您想隐藏状态栏吗?不,我只想让图像像这样位于状态栏下:不为我工作,仍在导航栏下