如何修复 - safeAreaLayoutGuide' 仅适用于 iOS 11.0 或更高版本

How to fix - safeAreaLayoutGuide' is only available on iOS 11.0 or newer

在我的应用程序中出现此错误 -

safeAreaLayoutGuide' is only available on iOS 11.0 or newer

在此代码中,错误出现了 3 次。基本上在我使用 safeArea 的每一行中。

NSLayoutConstraint.activate([
        stackViewBottomConstrols.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
        stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
        stackViewBottomConstrols.heightAnchor.constraint(equalToConstant: 50)
        ])

我可以只检查 IOS 11 是否可用和 运行 此代码并在 else 语句中添加另一个代码,代码相同但没有 safeArea。这会显示与 if 语句中相同的视图吗?如果没有,还有其他解决方案吗?

此代码能否在不具有 IOS 11 的设备上运行? -

        if #available(iOS 11.0, *) {
        NSLayoutConstraint.activate([
            stackViewBottomConstrols.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
            stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
            stackViewBottomConstrols.heightAnchor.constraint(equalToConstant: 50)
            ])
    } else {
        NSLayoutConstraint.activate([
            stackViewBottomConstrols.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            stackViewBottomConstrols.heightAnchor.constraint(equalToConstant: 50)
            ])
    }

safeAreaLayoutGuide 只是顶部、底部布局指南的替代品,添加了前导、尾随 - 当然你的代码很好用,这是创建的唯一方法代码中的约束以支持 IOS 11 和更低版本,但仅切换无关紧要的约束

stackViewBottomConstrols.heightAnchor.constraint(equalToConstant: 50)

成为这个

if #available(iOS 11.0, *) {
    NSLayoutConstraint.activate([
        stackViewBottomConstrols.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
        stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
     ])
} else {
    NSLayoutConstraint.activate([
        stackViewBottomConstrols.bottomAnchor.constraint(equalTo: self.bottomLayoutGuide.topAnchor),
        stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.trailingAnchor)
     ])
      
}

如果您想尽可能保持一致,请在 iOS-11 之前的块中,将 view.bottomAnchor 替换为 bottomLayoutGuide.topAnchor。在 iOS 11 之前,仍然有 topLayoutGuidebottomLayoutGuide 来帮助程序员考虑导航栏、标签栏等。否则,我觉得不错!

编辑:结果代码:

if #available(iOS 11.0, *) {
    NSLayoutConstraint.activate([
        stackViewBottomConstrols.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
        stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
        ])
} else {
    NSLayoutConstraint.activate([
        stackViewBottomConstrols.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor),
        stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        ])
}
// This one doesn't care which iOS version it is
stackViewBottomConstrols.heightAnchor.constraint(equalToConstant: 50)