1

I'm making a project and fighting with the problem with the black borders - I can't understand why my background is not on a full screen. It seems like the anchors are right, maybe the problem is in SceneDelegate, but I don't know exactly.

Here is my SceneDelegate code:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    self.window = UIWindow(frame: windowScene.coordinateSpace.bounds)
    self.window?.windowScene = windowScene
    window?.rootViewController = DetailsViewController()
    window?.makeKeyAndVisible()
}

And here is my ViewController:

private let detailsView = DetailsView() // view where backgroundImage and cat picture exist

private func setupLayout() { // method for setting constraints for view
    view.addSubview(detailsView)
    detailsView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
    detailsView.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor).isActive = true
}

If you know how to fix this problem, please share with me. I tried to Google it but failed.

Here is screenshot with the problem in Simulator

1 Answers1

0

You can set the constraints as below to make detailsView full screen. Here we are setting top,bottom,trailing and leading anchors of the detailsView to equal to those of view.safeAreaLayoutGuide

private func setupLayout() { // method for setting constraints for view
    view.addSubview(detailsView)
    let safeAreaGuide = self.view.safeAreaLayoutGuide
    NSLayoutConstraint.activate([
        detailsView.bottomAnchor.constraint(equalTo: safeAreaGuide.bottomAnchor),
        detailsView.topAnchor.constraint(equalTo: safeAreaGuide.topAnchor),
        detailsView.trailingAnchor.constraint(equalTo: safeAreaGuide.trailingAnchor),
        detailsView.leadingAnchor.constraint(equalTo: safeAreaGuide.leadingAnchor)
    ])
}
SamB
  • 1,560
  • 1
  • 13
  • 19