ios 安全区域布局指南不适用于情节提要中的UITableViewController

bkhjykvo  于 2023-08-08  发布在  iOS
关注(0)|答案(2)|浏览(133)

UITableViewController似乎没有iPhone X中的安全区域布局指南,无法将表格视图的顶部和底部边距设置为折叠底部布局。所有的视图控制器都工作正常,在故事板的顶部和底部边缘的安全区域布局指南。UITableView控制器除外。
我附上iPhoneX的截图作为参考。您可以看到字符串**“wheat”**不适合底部安全区域。


的数据

bq8i3lrv

bq8i3lrv1#

我相信这是因为视图控制器的视图(它总是占据整个屏幕大小)是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
我还没试过,但你可以给予。也许这就是你需要的开箱即用

v440hwme

v440hwme2#

这是我目前的修复,以强制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)
    }
}

相关问题