0

I want to put BlueView under NavigationBar on TableViewController. But it does not work.

like [1]: https://i.stack.imgur.com/40Crj.jpg

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)
        }
    }
}

I found that it works by using viewController with 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)
        }
    }
}

But I really want to know why? Or anything else ideas about safeAreaLayoutGuide on UITableViewController.

QueenaHuang
  • 11
  • 1
  • 2

1 Answers1

1

I think the tableViewController consists of a tableView covering everything, hence you wont be able to place a view in between the tableView and the navigationbar. You can try bringing your blueView on top of the tableView by using view.bringSubview(toFront: blueView)

Secondly your code is a bit off from the best practice.

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

This should have the exact same effect as your code but much shorter.

Edit: If this causes the view to be located below your navigationBar set edgesForExtendedLayout = [] in your viewDidLoad() or setup function

Vollan
  • 1,887
  • 11
  • 26