【问题标题】:How to remove "safeArea" appearance on the small screens in Swift? (Background is not on a full screen)如何删除 Swift 小屏幕上的“safeArea”外观? (背景不是全屏)
【发布时间】:2021-11-24 19:42:49
【问题描述】:

我正在制作一个项目并解决黑色边框的问题 - 我不明白为什么我的背景不是全屏显示。看起来锚点是对的,可能问题出在SceneDelegate,但我不知道具体。

这是我的 SceneDelegate 代码:

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()
}

这是我的 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
}

如果您知道如何解决此问题,请与我分享。我尝试谷歌搜索但失败了。

Here is screenshot with the problem in Simulator

【问题讨论】:

  • 嗨,你需要detailsView 全屏显示吗?
  • @SamB 嗨!是的,我需要在没有黑色边框的情况下全屏显示它

标签: swift uikit uiscenedelegate


【解决方案1】:

您可以设置如下约束以使detailsView 全屏显示。在这里,我们将detailsView 的顶部、底部、尾随和前导锚点设置为等于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)
    ])
}

【讨论】:

    猜你喜欢
    • 2012-03-27
    • 2022-10-02
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多