ios - 在 Snapkit 中,tableViewController 上的 safeAreaLayoutGuide 不起作用

标签 ios swift safearealayoutguide snapkit

我想将 BlueView 放在 TableViewController 上的 NavigationBar 下。但这不起作用。

像[1]:https://imgur.com/a/5WcQQ

class TableViewController: UITableViewController {
    private func setupView() {
        view.addSubview(blueView)

        blueView.translatesAutoresizingMaskIntoConstraints = false
        blueView.snp.makeConstraints { (make) in
            make.left.equalTo(view.snp.left)
            make.right.equalTo(view.snp.right)
            make.top.equalTo(view.safeAreaLayoutGuide.snp.topMargin)
            make.height.equalTo(50)
        }
    }
}

我发现它可以通过将 viewController 与 tableview 一起使用来工作。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView!
    private func setupView() {
        view.addSubview(blueView)

        blueView.translatesAutoresizingMaskIntoConstraints = false
        blueView.snp.makeConstraints { (make) in
            make.left.equalTo(view.snp.left)
            make.right.equalTo(view.snp.right)
            make.top.equalTo(view.safeAreaLayoutGuide.snp.topMargin)
            make.height.equalTo(50)
        }
    }
}

但是我很想知道为什么?或者关于 UITableViewController 上的 safeAreaLayoutGuide 的任何其他想法。

最佳答案

我认为 tableViewController 由覆盖所有内容的 tableView 组成,因此您无法在 tableView 和导航栏之间放置 View 。您可以尝试使用 view.bringSubview(toFront: blueView)

将 blueView 置于 tableView 之上

其次,您的代码有点偏离最佳实践。

blueView.snp.makeConstraints { (make) in
    make.leading.trailing.top.equalToSuperview()
    make.height.equalTo(50)
}

这应该与您的代码具有完全相同的效果,但要短得多。

编辑:如果这导致 View 位于 navigationBar 下方,请在 viewDidLoad() 中设置 edgesForExtendedLayout = [] >设置函数

关于ios - 在 Snapkit 中,tableViewController 上的 safeAreaLayoutGuide 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49879807/

相关文章:

ios - 自动布局和同心较小的嵌套 UIImageView

ios - 黑色 UICollectionView

iOS 9 和 10 : blank space added at the screen top while using safe area layout guide

ios - 在 Xcode 8/Swift 3 中按下 UIButton 时出现 "Unrecognized selector sent to instance"错误

ios - UITabBarController 中 tabBarItems 的大小

ios - 如何在 Core Data 中使用派生属性

ios - 通过另一个 ObObject 传递 ObservableObject 模型?

ios - Watch Complication 导致 dyld : Symbol not found: _OBJC_CLASS_$_CLKFullColorImageProvider 崩溃

ios - 强制更新 subview Controller 的 safeAreaInsets

ios - 如何在低于 11 的 iOS 中指定安全区域的偏移量?