在我的应用中,我有这个错误 -
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)
])
}
答案 0 :(得分:3)
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),
}
答案 1 :(得分:0)
如果您希望尽可能保持一致,请在iOS-11之前的版块中,将view.bottomAnchor
替换为bottomLayoutGuide.topAnchor
。在iOS 11之前,仍有topLayoutGuide
和bottomLayoutGuide
来帮助程序员考虑导航栏,标签栏等。否则,对我来说看起来不错!
编辑:结果代码:
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)