安全区域布局指南不适用于故事板中的UITableViewController

时间:2017-12-14 15:06:33

标签: ios uitableview uistoryboard iphone-x safearealayoutguide

似乎UITableViewController没有安全区域布局指南将表视图顶部和底部边距设置为与 iPhone X 中底部布局的碰撞,所有视图控制器都正常工作顶部和底部边距的故事板中的安全区域布局指南。除UITableView控制器外,请帮助解决此问题。

作为参考,我附上了 iPhoneX 的屏幕截图。在那里你可以看到字符串“wheat”不适合在底部安全区域内。

enter image description here

2 个答案:

答案 0 :(得分:11)

我相信这是因为视图控制器的视图(总是占用整个屏幕大小)是tableview

使用UIViewController并在其view下添加表格视图(并添加约束以尊重底部安全区域)

使用以下命令设置表视图内容插入:

tableView.contentInset = UIEdgeInsetsMake(0, 0, UIApplication.shared.keyWindow!.safeAreaInsets.bottom, 0.0);

如果您使用后者并且目标iOS版本低于11,请确保使用以下内容进行验证:

     var safeAreaBottom: CGFloat = 0.0 
     if #available(iOS 11.0, *) {
         safeAreaBottom = UIApplication.shared.keyWindow!.safeAreaInsets.bottom
     }

     tableView.contentInset = UIEdgeInsetsMake(0, 0, safeAreaBottom, 0.0);

我还注意到iOS 11中tableview的这个属性(但没有描述:/):insetsContentViewsToSafeArea

我还没试过,但你可以尝试一下。也许这就是您需要的开箱即用

答案 1 :(得分:2)

这是我目前在UITableViewController强制执行顶部和底部安全区域的修复方法。首先,您必须将其嵌入UINavigationController(如果不需要,请隐藏导航栏),然后:

override func viewDidLoad() {
    super.viewDidLoad()

    configureFakeSafeArea()
}

@available(iOS 11.0, *)
override func viewSafeAreaInsetsDidChange() {
    super.viewSafeAreaInsetsDidChange()

    topSafeAreaHeight?.constant = view.safeAreaInsets.top
    bottomSafeAreaHeight?.constant = view.safeAreaInsets.bottom
}

private var topSafeAreaHeight: NSLayoutConstraint?
private var bottomSafeAreaHeight: NSLayoutConstraint?

private func configureFakeSafeArea() {
    guard let view = navigationController?.view else {
        return
    }

    let topSafeArea = UIView()
    topSafeArea.backgroundColor = tableView.backgroundColor
    var topConstant: CGFloat = 0
    if #available(iOS 11.0, *) {
        topConstant = view.safeAreaInsets.top
    }
    topSafeAreaHeight = topSafeArea.heightAnchor.constraint(equalToConstant: topConstant)
    view.addSubview(topSafeArea, constraints: [
        topSafeArea.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        topSafeArea.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        topSafeArea.topAnchor.constraint(equalTo: view.topAnchor),
        topSafeAreaHeight!
    ])

    let bottomSafeArea = UIView()
    bottomSafeArea.backgroundColor = tableView.backgroundColor
    var bottomConstant: CGFloat = 0
    if #available(iOS 11.0, *) {
        bottomConstant = view.safeAreaInsets.bottom
    }
    bottomSafeAreaHeight = bottomSafeArea.heightAnchor.constraint(equalToConstant: bottomConstant)
    view.addSubview(bottomSafeArea, constraints: [
        bottomSafeArea.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        bottomSafeArea.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        bottomSafeArea.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        bottomSafeAreaHeight!
    ])
}

我们必须编写所有这些代码,这真是令人遗憾,所以如果有人知道更简单的方法,请告诉我们。

P.S。我在这里使用了这个小UIView扩展程序:

extension UIView {
    func addSubview(_ child: UIView, constraints: [NSLayoutConstraint]) {
        addSubview(child)
        child.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate(constraints)
    }
}